Python and Django Blog Sample Project -


i newbie python django, , have started sample project "blog".

currently blog posts added database manually, want @ front end providing form user. created model.py , views.py files, unable see these fields on front end. have copied of code below:

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     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 />         {% post in posts %}              <div class="posts">                 <h2>{{ post.title }}</h2>                 <h3>posted on {{ post.timestamp }} {{ 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', ) 

please let me know if missing anything. let me know how can achieve without using django model form.

thanks

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> 

update:

ok see why it's not submitting. put button instead of submit in input type

<input type="submit" name="btnsave" id="bntsave" value="submit" class="default2"/> //<--------------- 

just add auto_now_add in timestamp field

timestamp = models.datetimefield(auto_now_add=true)  

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -