发布时间:2025-12-10 19:14:03 浏览次数:6
初学django,写个菊花聊天室。简单无脑,没有美化。「终于解决」小生不会美化,看客勿喷。views.py:fromdjango.http.requestimportHttpRequestfromdjango.http.responseimportHttpResponsefromdjango.shortcutsimportrender_to_responsefromdjango.templat_ctf菊花聊天
小生不会美化,看客勿喷。
views.py:
from django.http.request import HttpRequestfrom django.http.response import HttpResponsefrom django.shortcuts import render_to_responsefrom django.template.loader import get_templateimport datetimefrom django.template import Template,Contextfrom jhlts.forms import liuyanFormfrom jhlts.liuyanban.models import liuyandef index(request): ends=[] errors=[] if request.method=='POST': form=liuyanForm(request.POST) if form.is_valid(): cd=form.cleaned_data t=liuyan(name=cd['name'],email=cd['email'],subject=cd['subject'],content=cd['content']) t.save() ends.append(cd) else: for i in form.errors: errors.append((i,form.errors[i])) says=[] s=liuyan.objects.all() for ss in s: says.append(ss) return render_to_response('c.html',{ 'current_date':datetime.datetime.now(), 'liuyankuang':liuyanForm().as_ul(), 'errors':errors, 'ends':ends, 'says':says, })希望我今天分享的这篇文章可以帮到您。
models.py:
from django.db import models# Create your models here.class liuyan(models.Model): name=models.CharField(max_length=20) email=models.EmailField() subject=models.CharField(max_length=30) content=models.CharField(max_length=100) forms.py
from django import formsclass liuyanForm(forms.Form): name=forms.CharField(max_length=20) email=forms.EmailField(required=False) subject=forms.CharField(max_length=30) content=forms.CharField(widget=forms.Textarea,max_length=100) def clean_name(self): name=self.cleaned_data['name'] if len(name)<4: raise forms.ValidationError('Not Enough words!') return nameurls.py
from django.conf.urls import patterns, include, url# Uncomment the next two lines to enable the admin:# from django.contrib import admin# admin.autodiscover()urlpatterns = patterns('', url(r'^$','jhlts.views.index'), url(r'^index$','jhlts.views.index'), # Examples: # url(r'^$', 'jhlts.views.home', name='home'), # url(r'^jhlts/', include('jhlts.foo.urls')), # Uncomment the admin/doc line below to enable admin documentation: # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), # Uncomment the next line to enable the admin: # url(r'^admin/', include(admin.site.urls)),)
index.html
<!DOCTYPE HTML PUBLIC "- //W3C//DTD HTML 4.01//EN"><html><title>jhlts</title><body>{%block time%}{% endblock %}{%block errors%}{% endblock %}<hr>{%block liuyan%}{% endblock %}<hr>{%block showsays%}{% endblock %}</body></html>c.html
{%extends "index.html" %}{%block time%}The Current time is { {current_date}}{% endblock %}{%block errors%}{% if errors %}<hr>errors:<ul>{%for error in errors%}<li>{%for i in error%}{ {i}}{%endfor%}</li>{%endfor%}</ul>{%endif%}{% endblock %}{%block ends%}{% endblock %}{%block liuyan%}<p>Welcome to 菊花聊天室</p><form action="" method="post">{ {liuyankuang}}<input type="submit" value="Submit"></form>{% endblock %}{%block showsays%}<p>people says:</p>{%for i in says%}<UL><LI>姓名:{ {i.name}}<br>{%if i.email%}邮箱:{ {i.email}} <br>{%endif%}主题:{ {i.subject}}<br>内容:{ {i.content}}</LI></UL><hr> {%endfor%}{%endblock %}