初学django,写个菊花聊天室。简单无脑,没有美化。

小生不会美化,看客勿喷。





views.py:

from django.http.request import HttpRequest
from django.http.response import HttpResponse
from django.shortcuts import render_to_response
from django.template.loader import get_template
import datetime
from django.template import Template,Context
from jhlts.forms import liuyanForm

from jhlts.liuyanban.models import liuyan

def 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 forms

class 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 name


urls.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 %}








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值