django-admin startproject projectname
python manage.py startapp appname
python manage.py runserver
python manage.py migrate
python manage.py makemigrations
python manage.py createsuperuser
python manage.py shell
myproject/
├── manage.py
├── myproject/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── wsgi.py
│ └── asgi.py
└── appname/
├── __init__.py
├── admin.py
├── apps.py
├── models.py
├── tests.py
├── views.py
├── urls.py # (create this file manually)
└── migrations/
INSTALLED_APPS = [
# other apps
'appname',
]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dbname',
'USER': 'username',
'PASSWORD': 'password',
'HOST': 'localhost',
'PORT': '',
}
}
STATIC_URL = '/static/'
STATICFILES_DIRS = [BASE_DIR / "static"]
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / "media"
from django.db import models
class ModelName(models.Model):
fieldname = models.CharField(max_length=255)
integer_field = models.IntegerField()
date_field = models.DateField()
def __str__(self):
return self.fieldname
CharField(max_length=255)
TextField()
IntegerField()
FloatField()
BooleanField(default=False)
DateField()
DateTimeField(auto_now_add=True, auto_now=True)
ForeignKey(ModelName, on_delete=models.CASCADE)
ManyToManyField(ModelName)
python manage.py makemigrations
python manage.py migrate
from django.shortcuts import render
def my_view(request):
context = {"key": "value"}
return render(request, 'template.html', context)
from django.views import View
class MyView(View):
def get(self, request):
context = {"key": "value"}
return render(request, 'template.html', context)
from django.http import HttpResponse, Http404, JsonResponse
def my_view(request):
return HttpResponse("Hello, World!")
def my_json_view(request):
return JsonResponse({"key": "value"})
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('appname/', include('appname.urls')),
]
from django.urls import path
from . import views
urlpatterns = [
path('', views.my_view, name='home'),
path('detail/<int:id>/', views.detail_view, name='detail'),
]
from django.shortcuts import render
def my_view(request):
return render(request, 'template.html', {"key": "value"})
<!-- base.html -->
<!DOCTYPE html>
<html>
<head>
<title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
<!-- child_template.html -->
{% extends "base.html" %} {% block title %}Child Template{% endblock %} {% block
content %}
<h1>Content from Child Template</h1>
{% endblock %}
{% if condition %}
<p>Condition is True</p>
{% else %}
<p>Condition is False</p>
{% endif %} {% for item in list %}
<p>{{ item }}</p>
{% endfor %} {{ variable }} {{ variable|filter }}
from django import forms
class MyForm(forms.Form):
name = forms.CharField(max_length=255)
email = forms.EmailField()
from .forms import MyForm
def my_view(request):
if request.method == 'POST':
form = MyForm(request.POST)
if form.is_valid():
# process form data
pass
else:
form = MyForm()
return render(request, 'template.html', {'form': form})
from django.contrib import admin
from .models import ModelName
@admin.register(ModelName)
class ModelNameAdmin(admin.ModelAdmin):
list_display = ('field1', 'field2')
search_fields = ('field1',)
from django.contrib.auth import authenticate, login, logout
# Authenticate User
user = authenticate(request, username='username', password='password')
# Log In
if user is not None:
login(request, user)
# Log Out
logout(request)
from django.contrib.auth.decorators import login_required
@login_required
def my_view(request):
return render(request, 'template.html')
python manage.py collectstatic
gunicorn myproject.wsgi:application --bind 0.0.0.0:8000
server {
listen 80;
server_name yourdomain.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /path/to/staticfiles;
}
location /media/ {
root /path/to/media;
}
location / {
include proxy_params;
proxy_pass http://unix:/path/to/yourproject.sock;
}
}