Django+mysql User Login system, encounter problems and solve

Source: Internet
Author: User

Add to:

Programmingerror: (1146, "Table ' test.app_name_testmodel ' doesn ' t exist") Resolved: 

1. 创建更改的文件python manage.py makemigrations

2. 将生成的py文件应用到数据库  python manage.py migrateDjango __init__ () takes exactly 1 argument (2 given) has two parameter values tuned to onenot resolved

Django Common commands:

1. Create a new Django project:django-admin.py startproject project_name

2. Create a new app:python manage.py startapp app_name

3. Create a database table or change a database table or field :python manage.py makemigrations

python manage.py migrate

4. Use the development server :python manage.py runserver 0.0.0.0:8000

5. 清空数据库:python manage.py flush6. Create a Super Administrator: python manage.py createsuperuserTo modify a user password: python manage.py changepassword username7. Export Data import data: python manage.py dumpdata appname > appname.json python manage.py loaddata appname.json8. Django Project Environment Terminal: python manage.py shell

Create a new Django project

or also: django-admin.py startproject mysqllogin

新建APP:python manage.py startapp app01

app01>models.py

 #  -*-coding:utf-8-*- from  __future__  import   unicode_literals  from  django.db Span style= "COLOR: #0000ff" >import   models   #   Create your models here.  class   User (models. Model): Username  =models. Charfield (Max_length=32 =models. Charfield (Max_length=32)  def  __unicode__    return  Self.username 


app01>views.py

#Coding=utf-8 fromDjango.shortcutsImportRender,render_to_response fromDjango.httpImportHttpresponse,httpresponseredirect fromDjango.templateImportRequestContext fromDjangoImportForms fromModelsImportUser#formclassUserForm (forms. Form): Username= Forms. Charfield (label='User name', max_length=100) Password= Forms. Charfield (label='Password', widget=forms. Passwordinput ())#Registerdefregist (req):ifReq.method = ='POST': UF=UserForm (req. POST)ifuf.is_valid ():#Get form dataUsername = uf.cleaned_data['username'] Password= uf.cleaned_data['Password']            #Add to DatabaseUser.objects.create (Username= username,password=password)returnHttpResponse ('regist success!!')    Else: UF=UserForm ()returnRender_to_response ('regist.html',{'uf': uf})#LogindefLogin (req):ifReq.method = ='POST': UF=UserForm (req. POST)ifuf.is_valid ():#get form User passwordUsername = uf.cleaned_data['username'] Password= uf.cleaned_data['Password']            #get the form data to compare to the databaseuser = User.objects.filter (username__exact = Username,password__exact =password)ifUser:#more successful, jump indexResponse = Httpresponseredirect ('index.html')                #writes username to the browser cookie with an expiration time of 3600Response.set_cookie ('username', username,3600)                returnResponseElse:                #failed to compare, still in login                returnHttpresponseredirect ('login.html')    Else: UF=UserForm ()returnRender_to_response ('login.html',{'uf': uf})#Landing SuccessdefIndex (req): Username= req. Cookies.get ('username',"')    returnRender_to_response ('index.html',{'username': username})#ExitdefLogout (req): Response= HttpResponse ('Logout !')    #save username in cookie cleanupResponse.delete_cookie ('username')    returnResponse

mysqllogin>setting.py

 fromDjango.conf.urlsImportURL fromDjango.contribImportAdmin fromApp01ImportViewsurlpatterns= [    #URL (r ' ^admin/', admin.site.urls),URL (r'^index/', views.index), url (r'^login/', views.login), url (r'^regist/', views.regist), url (r'^logout/', views.logout), url (r'^$', Views.login),]

Templates>index.html

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >< HTML xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" lang= "en" >

Templates>login.html

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >< HTML xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" lang= "en" >    {% csrf_token%  }    {{uf.as_p}}    <input type= "submit" value = "OK"/></form><br><a href= "http/ 127.0.0.1:8000/regist/"> Registration </a></body>

Templates>regist.html

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" >< HTML xmlns= "http://www.w3.org/1999/xhtml" xml:lang= "en" lang= "en" >    {% csrf_token%}    {{uf.as_p}}     <input type= "Submit" value = "OK"/></form><br><a href= "http://127.0.0.1:8000/login/" > Login < /a></body>

Python manage.py Shell

From App01.models import User

Query All Users: User.objects.all ()

Added User: User.objects.create (username= ' xxx ', password= ' xxx ')

Django+mysql User Login system, encounter problems and solve

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.