django 用户注册

环境:
python 2.7.3
django 1.7.7
数据库:sqlite3

1.新建项目
django-admin.py startproject django_3
cd django_3

2.新建app
python manage.py startapp detail

3.同步数据库并创建superuser用户
python manage.py makemigrations
python manage.py migrate
python manage.py syncdb
按照提示输入

4.修改app detail下models.py文件
from django.db import models
from django.contrib import admin
# Create your models here.
class Detail_User(models.Model):
    username = models.CharField(max_length=50)
    password = models.CharField(max_length=50)
class UserAdmin(admin.ModelAdmin):
    list_display = ('username','password')
admin.site.register(Detail_User,UserAdmin)
创建Detail_User表包含username与password,并注册到后台

5.修改app 下views.py文件
from django.shortcuts import render
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from detail.models import Detail_User
from django import forms

class UserForm(forms.Form):
    username = forms.CharField(label='username',max_length=100)
    password = forms.CharField(label='password',widget=forms.PasswordInput())
def signup(request):
    if request.method == "POST":
        uf = UserForm(request.POST)
        if uf.is_valid():
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            if username != None and password != None:
                user = Detail_User()
                user.username = username
                user.password = password
                user.save()
                return render(request, 'index.html', locals())
            else:
                return HttpResponseRedirect('/signup.html/')
    else:
        uf = UserForm()
    return render(request, 'signup.html', locals())   
6.修改django_3的settings.py文件
添加app
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'detail',
)  
注释掉csrf
MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
) 

7.修改django_3下的urls.py文件
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    # Examples:
    # url(r'^$', 'django_3.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^signup.html/$','detail.views.signup'),
)
8.同步数据库
python manage.py makemigrations
python manage.py migrate  

9.编写模板文件signup.html
<!DOCTYPE html>
<html class="login-bg">
<head>
   <title>Detail Admin - Sign up</title>
    
   <meta name="viewport" content="width=device-width, initial-scale=1.0" />
   
    <!-- bootstrap -->
    <link href="http://127.0.0.1/css/bootstrap/bootstrap.css" rel="stylesheet" />
    <link href="http://127.0.0.1/css/bootstrap/bootstrap-responsive.css" rel="stylesheet" />
    <link href="http://127.0.0.1/css/bootstrap/bootstrap-overrides.css" type="text/css" rel="stylesheet" />

    <!-- global styles -->
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1/css/layout.css" />
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1/css/elements.css" />
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1/css/icons.css" />

    <!-- libraries -->
    <link rel="stylesheet" type="text/css" href="http://127.0.0.1/css/lib/font-awesome.css" />
    
    <!-- this page specific styles -->
    <link rel="stylesheet" href="http://127.0.0.1/css/compiled/signin.css" type="text/css" media="screen" />

    <!-- open sans font -->
    <link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css' />

    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head>
<body>
    <div class="header">
    </div>
    <div class="row-fluid login-wrapper">
        <div class="box">
            <div class="content-wrap">
                <h6>Sign Up</h6>
                    <form method = 'post' enctype="multipart/form-data">
                        {{ uf.as_p }}
                        <input type="submit" value = "Sign up" />
                    </form>
                </div>
            <p>Already have an  account?</p>
            <a href="http://127.0.0.1:8000/signin.html">Sign in</a>
        </div>
        </div>

    </div>

   <!-- scripts -->
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script src="http://127.0.0.1/js/bootstrap.min.js"></script>
    <script src="http://127.0.0.1/js/theme.js"></script>
        <!-- pre load bg imgs -->
    <script type="text/javascript">
        $(function () {
            // bg switcher
            var $btns = $(".bg-switch .bg");
            $btns.click(function (e) {
                e.preventDefault();
                $btns.removeClass("active");
                $(this).addClass("active");
                var bg = $(this).data("img");

                $("html").css("background-image", "url('img/bgs/" + bg + "')");
            });

        });
    </script>
<div style="display:none"><script src='http://v7.cnzz.com/stat.php?id=155540&web_id=155540' language='JavaScript' charset='gb2312'></script></div>
</body>
</html>

10.启动项目
python manage.py runserver

django 用户注册_第1张图片

django 用户注册_第2张图片

django 用户注册_第3张图片

输入用户名和密码:eddy,123456
点击sign up
通过后台可以看eddy:123456已经写入到数据库中


你可能感兴趣的:(django 用户注册)