django+mysql使用者登入系統,遇到問題及解決

來源:互聯網
上載者:User

標籤:使用者密碼   mit   info   pre   create   使用者登入   label   value   def   

添加:

ProgrammingError: (1146, "Table ‘test.app_name_testmodel‘ doesn‘t exist")    解決: 

1. 建立更改的檔案python manage.py makemigrations

2. 將產生的py檔案應用到資料庫  python manage.py migratedjango  __init__() takes exactly 1 argument (2 given)   有兩個參數值調到一個  未解決   

django常用命令:

1. 建立一個 django project:django-admin.py startproject project_name

2. 建立 app:python manage.py startapp app_name

3. 建立資料庫表 或 更改資料庫表或欄位:python manage.py makemigrations

python manage.py migrate

4. 使用程式開發伺服器:python manage.py runserver 0.0.0.0:8000

5. 清空資料庫:python manage.py flush6. 建立超級管理員: python manage.py createsuperuser修改使用者密碼: python manage.py changepassword username7. 匯出資料 匯入資料: python manage.py dumpdata appname > appname.json python manage.py loaddata appname.json8. Django 項目環境終端: python manage.py shell

建立一個django項目

或者亦可:django-admin.py startproject mysqllogin

建立APP:python manage.py startapp app01

app01>models.py

# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.db import models# Create your models here.class User(models.Model):    username=models.CharField(max_length=32)    password=models.CharField(max_length=32)    def __unicode__(self):        return self.username


app01>views.py

#coding=utf-8from django.shortcuts import render,render_to_responsefrom django.http import HttpResponse,HttpResponseRedirectfrom django.template import RequestContextfrom django import formsfrom models import User#表單class UserForm(forms.Form):    username = forms.CharField(label=‘使用者名稱‘,max_length=100)    password = forms.CharField(label=‘密碼‘,widget=forms.PasswordInput())#註冊def regist(req):    if req.method == ‘POST‘:        uf = UserForm(req.POST)        if uf.is_valid():            #獲得表單資料            username = uf.cleaned_data[‘username‘]            password = uf.cleaned_data[‘password‘]            #添加到資料庫            User.objects.create(username= username,password=password)            return HttpResponse(‘regist success!!‘)    else:        uf = UserForm()    return render_to_response(‘regist.html‘,{‘uf‘:uf})#登陸def login(req):    if req.method == ‘POST‘:        uf = UserForm(req.POST)        if uf.is_valid():            #擷取表單使用者密碼            username = uf.cleaned_data[‘username‘]            password = uf.cleaned_data[‘password‘]            #擷取的表單資料與資料庫進行比較            user = User.objects.filter(username__exact = username,password__exact = password)            if user:                #比較成功,跳轉index                response = HttpResponseRedirect(‘index.html‘)                #將username寫入瀏覽器cookie,失效時間為3600                response.set_cookie(‘username‘,username,3600)                return response            else:                #比較失敗,還在login                return HttpResponseRedirect(‘login.html‘)    else:        uf = UserForm()    return render_to_response(‘login.html‘,{‘uf‘:uf})#登陸成功def index(req):    username = req.COOKIES.get(‘username‘,‘‘)    return render_to_response(‘index.html‘ ,{‘username‘:username})#退出def logout(req):    response = HttpResponse(‘logout !!‘)    #清理cookie裡儲存username    response.delete_cookie(‘username‘)    return response

mysqllogin>setting.py

from django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsurlpatterns = [    # 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"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <title></title></head><body><h1>welcome {{username}} !</h1><br><a href="http://127.0.0.1:8000/login/">退出</a></body></html>

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"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <title>登陸</title></head><body><h1 style="background-color: aqua">登陸頁面:</h1><form method = ‘post‘ enctype="multipart/form-data">    {% csrf_token %}    {{uf.as_p}}    <input type="submit" value = "ok" /></form><br><a href="http://127.0.0.1:8000/regist/">註冊</a></body></html>

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"><head>    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />    <title>註冊</title></head><body><h1>註冊頁面:</h1><form method = ‘post‘ enctype="multipart/form-data">    {% csrf_token %}    {{uf.as_p}}    <input type="submit" value = "ok" /></form><br><a href="http://127.0.0.1:8000/login/">登陸</a></body></html>

python manage.py shell

from app01.models import User

查詢全部使用者:User.objects.all()

新增使用者:User.objects.create(username=‘xxx‘,password=‘xxx‘)

django+mysql使用者登入系統,遇到問題及解決

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.