📅  最后修改于: 2023-12-03 15:30:30.239000             🧑  作者: Mango
本备忘单主要介绍Django开发中常用的知识点和技巧,方便程序员快速查阅。
django-admin startproject <project-name>
python manage.py startapp <app-name>
python manage.py makemigrations
python manage.py migrate
python manage.py createsuperuser
python manage.py dumpdata <app-name> > <app-name>.json
python manage.py loaddata <app-name>.json
from django.shortcuts import render, HttpResponse
def index(request):
return HttpResponse('Hello, world!')
from django.shortcuts import render
def index(request):
title = 'Django备忘单'
return render(request, 'index.html', {'title': title})
{% for item in data %}
{{ item }}
{% endfor %}
{% if condition %}
...
{% else %}
...
{% endif %}
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
from django.urls import path
from . import views
urlpatterns = [
path('post/<int:pk>/', views.post_detail, name='post_detail'),
]
在settings.py中添加静态文件目录:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
{% load static %}
<img src="{% static 'logo.png' %}" alt="Logo">
以上就是Django开发中常用的知识点和技巧,希望能对程序员们有所帮助。