当前位置:编程学习 > 网站相关 >>

Django项目15

1.准备工作。sql,project,app,admin,(templates,static)后面两个都在app内部。
2.settings
[python] 
DATABASES = {   
    'default': {  
        'ENGINE': 'django.db.backends.sqlite3',   
        'NAME': 'my.db',                                 
    }     
}  
  
TIME_ZONE = 'Asia/Shanghai'  
LANGUAGE_CODE = 'zh-cn'  
  
MIDDLEWARE_CLASSES = (  
    'django.middleware.common.CommonMiddleware',  
    'django.contrib.sessions.middleware.SessionMiddleware',  
    #'django.middleware.csrf.CsrfViewMiddleware',  
    'django.contrib.auth.middleware.AuthenticationMiddleware',  
    'django.contrib.messages.middleware.MessageMiddleware',  
)  
  
INSTALLED_APPS = (  
    'django.contrib.auth',  
    'django.contrib.contenttypes',  
    'django.contrib.sessions',  
    'django.contrib.sites',  
    'django.contrib.messages',  
    'django.contrib.staticfiles',  
    'django.contrib.admin',  
    'blog',  
)  
3.urls
[python]  
from django.conf.urls.defaults import patterns, include, url   
from django.contrib import admin  
admin.autodiscover()  
  
urlpatterns = patterns('',  
    url(r'^regist/$','blog.views.regist'),  
    url(r'^fileup/$','blog.views.fileup'),  
    url(r'^fileup2/$','blog.views.fileup2'),  
    url(r'^admin/', include(admin.site.urls)),  
)  
 
views
[python]  
#coding:utf8  
from blog.models import User  
from django.http import HttpResponse  
from django.shortcuts import render_to_response  
from django import forms  
  
class UserForm(forms.ModelForm):  
    class Meta:  
        model = User  
def regist(req):  
    if req.method == 'POST':  
        userform = UserForm(req.POST,req.FILES)#既是数据库对象,又是表单对象  
        if userform.is_valid():  
            userform.save()  
            return HttpResponse('ok')  
    else:  
        userform = UserForm()  
    return render_to_response('regist.html',{'userform':userform})  
def fileup(req):  
    f= req.FILES.get('bighead')#获取的是文件句柄  
    s= f.read()  
    s= '<pre>'+s+'</pre>'  
    fp = file('./note2013','wb')#二进制方式保存  
    fp.write(s)  
    fp.close()  
    return HttpResponse(s)  
  
class MyForm(forms.Form):  
    name = forms.CharField()  
    note = forms.FileField()  
  
  
def fileup2(req):  
    if req.method == 'POST':  
        form = MyForm(req.POST,req.FILES)  
        if form.is_valid():  
            name = req.POST.get('name')  
            note = req.FILES.get('note')  
            s = note.read()  
            fp = file('note01','wb')  
            fp.write(s)  
            fp.close()  
            return HttpResponse('ok')  
    else:  
        form = MyForm()  
    return render_to_response('fileup2.html',{'form':form})  
models
[python]  
from django.db import models  
class User(models.Model):  
    name = models.CharField(max_length=20)  
    易做图 =models.CharField(choices=(('m','male'),('f','female')),max_length=1)  
    myfile = models.FileField(upload_to='upfile/')  
    def __unicode__(self):  
        return self.name  
static/f.html
[html]  
<body>  
<form  method="post" enctype="multipart/form-data">  
{{form.as_p}}  
<input type="submit" />  
</form>  
</body>  
 
templates/fileup2.html &®ist.html
[plain] view plaincopyprint?
<body>  
<form action="/fileup/" method="post" enctype="multipart/form-data">  
<input type="file" name="bighead" /><br />  
<input type="submit" />  
</form>  
</body>  
[html] 
<body>  
<form method="POST" enctype="multipart/form-data">   
{{userform.as_p}}  
<input type="submit" value="注册">  
</form>  
</body>  
补充:Web开发 , Python ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,