Python和Django博客示例项目

时间:2013-04-09 07:42:34

标签: django django-models python-2.7 django-forms django-views

我是python以及Django的新手,我已经开始了一个示例项目“博客”。

目前博客文章已手动添加到数据库中,但我希望通过向用户提供表单来在前端执行此操作。我创建了model.pyviews.py个文件,但我无法在前端看到这些字段。我已经复制了下面的所有代码:

models.py

class posts(models.Model): 
    author = models.CharField(max_length = 30) 
    title = models.CharField(max_length = 100) 
    bodytext = models.TextField() 
    timestamp = models.DateTimeField() 

class postForm(ModelForm): 
    class Meta: 
         model = posts

views.py

def home(request):
    content = posts.objects.all()[:5]
    return render_to_response('index.html',{'posts' : content})

def save_blog(request):      
    form = postForm
    if request.POST:
        form = postForm(request.POST)
        if form.is_valid():
            form.save
    return render_to_response('index.html',{'form' : form},context_instance=RequestContext(request))

url.py

from django.conf.urls import patterns, include, url    
from django.contrib import admin   
admin.autodiscover()

urlpatterns =
    patterns('',
        url(r'^$', 'blog.views.home', name='home'),
        url(r'^admin/', include(admin.site.urls))
    )

的index.html

<body>
    <div class="container">
        <h1>Welcome To</h1>
        <hr />
        {% for post in posts %} 
            <div class="posts">
                <h2>{{ post.title }}</h2>
                <h3>Posted on {{ post.timestamp }} by {{ post.author }}</h3>
                <p>{{ post.bodytext }}</p>
            </div>
            <hr />
        {% endfor %}
    </div>
    <div class="forms">
        <form action="." method="post" name="posts" id="posts">{% csrf_token %}
        <table>
        <tr><td>{{form.author.label}}</td><td>{{form.author}}</td></tr>
        <tr><td>{{form.title.label}}</td><td>{{form.title}}</td></tr>
        <tr><td>{{form.bodytext.label}}</td><td>{{form.bodytext}}</td></tr>
        <tr><td></td><td><input type="button" name="btnSave" id="bntSave" value="Submit"  class = "default2"/></td>
        </table>
        </form>
    </div>
</body>

settings.MIDDLEWARE_CLASSES

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

如果我错过了什么,请告诉我。另请告诉我如何在不使用Django模型的情况下实现此目的。

由于

2 个答案:

答案 0 :(得分:0)

您似乎在调用表单postForm,但在视图中使用了posts_form ...

我也不确定form_save是什么,你应该在表单实例上调用save(),如

if form.is_valid():
    form.save()

发布后,通常会将用户重定向到另一个网址,以防止重新提交数据。

答案 1 :(得分:0)

views.py

def home(request):
     content = posts.objects.all()[:5]
     form = postForm() 
     if request.POST:
         form = postForm(request.POST) 
         if form.is_valid():
             form.save()
     return render_to_response('index.html',{'posts' : content, 'form' : form}, context_instance=RequestContext(request))

的index.html

<div class="forms">
    <form method="post" name="posts" id="posts">
        {% csrf_token %}
        {{ form.as_table }}
        <input type="button" name="btnSave" id="bntSave" value="Submit" class="default2"/>    //<---------------
    </form>
</div>

更新:

好的,我现在看到它为什么不提交。您在输入类型

中添加了button而不是submit
<input type="submit" name="btnSave" id="bntSave" value="Submit" class="default2"/> //<---------------

只需在时间戳字段

中添加auto_now_add即可
timestamp = models.DateTimeField(auto_now_add=True)