Django -- some config
1.主项目下的url配置:urls.py文件
from django.contrib import admin
from django.urls import path, include
from django.conf.urls import url
from django.views.generic import TemplateView
urlpatterns = [
path('admin/', admin.site.urls),
url('^appName/', include('appName.urls')),
url(r'^index/', TemplateView.as_view(template_name="index.html")),
# url(r'^xingyunwa/', TemplateView.as_view(template_name="lucky.html")),
]
2.settings.py文件
1)允许什么ip可以访问:
ALLOWED_HOSTS = [
'127.0.0.1',
]
2)加入创建的APP:如appName
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'appName',
]
3)配置数据库链接
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME':'qfautodb',
'USER':'root',
'PASSWORD':'root',
'HOST':'localhost',
'PORT':'3306',
},
}
4)静态文件的配置:
STATIC_URL = '/static/'
# STATIC_ROOT = os.path.join(BASE_DIR, "collectstatic")
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), 'commonstatic/static/' ]
3.app中的url.py文件配置:如appName
from django.conf.urls import url
from appName.views import home
urlpatterns = [
url('^home', home, name='home'),
]
HTML页面中引用:
引用url:href="{% url 'home' %} # 对应name的值
引用数据:{{ variable_name }} # 后端返回数据的变量名或字典key
4.django寻找HTML文件的路径:
1)本项目下的templates目录下
2)各app下的templates目录下
5.django寻找静态文件路径:
1)本项目下的static目录下
2)settings.py配置文件中的目录:
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"), 'commonstatic/static/' ]