Django怎么生成页面内容为txt

王雅宁 发布于 2015/04/17 15:09
阅读 763
收藏 0

【Gopher China万字分享】华为云的Go语言云原生实战经验!>>>

现在页面有一些表单,填写内容后希望提交,跟着页面原来的内容,生成一个新的html或者txt返回给用户

views。py里面应该怎么写




#coding: gb2312
from django.http import HttpResponse
import datetime,calendar
import time
from django.http import HttpResponse
from django.template import Context
from django.template.loader import get_template
from django.http import HttpResponse, Http404
from django.contrib.auth.models import User
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django.contrib.auth import logout
from django.template import RequestContext
#from django import form

from django.shortcuts import render
from .forms import ContactForm
#from django.shortcuts import render_to_response
#from django_manage_app.forms import ContactForm

def current_datetime(request):
 now = time.strftime('%Y-%m-%d-%H-%M-%S',time.localtime(time.time()))
 html = '<html><body>It is now %s.</body></html>' %now
 return HttpResponse(html)
 
def contact_author(request):
    if request.method == 'POST':#提交请求时才会访问这一段,首次访问页面时不会执行
        form = ContactForm(request.POST)
        if form.is_valid():#说明各个字段的输入值都符合要求
            cd = form.cleaned_data#只有各个字段都符合要求时才有对应的cleaned_data
            #print (form.cleaned_data())
            print (cd['subject'])
            print (cd['email'])
            print (cd['message'])
            return HttpResponseRedirect('/thanks/')
        else:#有部分字段不符合要求,会有error相关信息给加到form中去,需要覆盖掉
            #print (form)
            print ('The data does not meet the requirements')
            print (form['subject'].errors)
            print (form['email'].errors)
            print (form['message'].errors)
    else:#首次访问该url时没有post任何表单
        form = ContactForm()#第一次生成的form里面内容的格式
        print (form)
        print (form.is_valid())

    #“首次访问”和“提交的信息不符合要求”时被调用
    return render_to_response('contact_author.html', {'form': form})


def thanks(request):

    return render_to_response('thanks.html')
 
 
def download_file(request):  
 #from django.http import HttpResponse         
    ## CSV 
    #import csv     
    #response = HttpResponse(mimetype='text/csv') 
    #response['Content-Disposition'] = 'attachment; filename=my.csv' 
    #writer = csv.writer(response) 
    #writer.writerow(['First row', 'Foo', 'Bar', 'Baz']) 
    #writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"]) 
 
    # Text file 
 #response = HttpResponse()                                  
    #response['Content-Disposition'] = 'attachment; filename=my.txt'               
    #response.write("aa/n") 
    #response.write("bb")  
    
    # PDF file  
    #http://code.djangoproject.com/svn/django/branches/0.95-bugfixes/docs/outputting_pdf.txt 
    #from reportlab.pdfgen import canvas  #need pip ind
    #response = HttpResponse()#)mimetype='application/pdf') 
    #response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' 
    #p = canvas.Canvas(response) 
    #p.drawString(100, 100, "Hello world.") 
    #p.showPage() 
    #p.save() 
    fout=open("test.txt","wt")
    print >>fout,"hello world"
    fout.close()    
    response['Content-Disposition'] = 'attachment; filename=test.txt'
    return HttpResponse(response)

加载中
0
qiufsd
qiufsd
response['Content-Type'] = 'text/plain'

你可以在这里查一下,这个字段的其他选项。

0
扑棱扑棱
扑棱扑棱

return HttpResponse("txt内容")

返回顶部
顶部