一、項目結構(eclipse+PyDev工具)(粘上來後代碼縮進格式沒有了,就先不整了)html
二、關鍵代碼:mysql
test_log03.settings.pysql
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
)數據庫
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
#'django.middleware.csrf.CsrfViewMiddleware', #注意把這裏註釋掉(CSRF)
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)django
#注意同步數據庫,生成表格後才能在後期錄入數據session
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test03',
'HOST':'localhost',
'PORT':'3306',
'USER':'root',
'PASSWORD':'passwd',
}
}eclipse
test_log03.urls.py工具
<body>
<center><h1>hello,register!</h1></center>post
<!--注意post方法-->
<form method="post" >
{{uf.as_p}}url
<input type="submit" value="OK"/>
</form>
</body>
blog.models.py
from django.db import models
class User(models.Model):
username = models.CharField(max_length=20)
password = models.CharField(max_length=30)
def __unicode__(self):
return self.username
blog.views.py
from django.shortcuts import render, render_to_response
from django import forms
from django.http import HttpResponse
from blog.models import User
class UserForm(forms.Form):
username = forms.CharField()
password = forms.CharField()
def register(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() user.username = username user.password = password user.save() print username,password return HttpResponse('ok') else : uf = UserForm() return render_to_response('register.html',{'uf':uf})