- 使用专业版pycharm直接创建项目,项目名称untitled,APP名称my
- 设置url.py
- 项目
from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/',admin.site.urls), url(r'^my/', include('my.urls')),#项目路径中加入my的路径 ]
-
APP
from django.conf.urls import url from . import views urlpatterns ={ #url(r'^test$',views.index,name ='index'), #前台登陆页面 url(r'^login/$', views.login, name='login'), #登录验证 url(r'^do_login/$', views.do_login, name='do_login'), }
- 项目
- 3.界面在templates中,login.html .index.html
4.编写view.py
from django.shortcuts import render
from django.http import HttpResponse
#加载登录页面
def login (request):
return render(request,'login.html')
# 验证登录
def do_login(request):
username = request.POST.get('username')
password = request.POST.get('password')
print(username+" "+password)
if username == 'admin' and password == '123456':
return render(request, 'index.html')
else:
return render(request, 'login.html', {
'username': username,
'password': password
})
错误主要
- setting.py 里添加模板,按照网上添加 'DIRS': [os.path.join(BASE_DIR,'templates')],依然错误,将地址更改为绝对地址后,运行成功。
- 未注册APP