Note that there are some explanatory texts on larger screens.

plurals
  1. POPython and Django Blog Sample Project
    primarykey
    data
    text
    <p>I am newbie to python as well as Django, and I have started a sample project "blog".</p> <p>Currently blog posts are added to the database manually, but I want to do it at front end by providing a form to the user. I created my <code>model.py</code> and <code>views.py</code> files, but I am unable to see these fields on the front end. I have copied all of my code below:</p> <p><strong>models.py</strong>:</p> <pre><code>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 </code></pre> <p><strong>views.py</strong>:</p> <pre><code>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)) </code></pre> <p><strong>url.py</strong>:</p> <pre><code>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)) ) </code></pre> <p><strong>index.html</strong>:</p> <pre><code>&lt;body&gt; &lt;div class="container"&gt; &lt;h1&gt;Welcome To&lt;/h1&gt; &lt;hr /&gt; {% for post in posts %} &lt;div class="posts"&gt; &lt;h2&gt;{{ post.title }}&lt;/h2&gt; &lt;h3&gt;Posted on {{ post.timestamp }} by {{ post.author }}&lt;/h3&gt; &lt;p&gt;{{ post.bodytext }}&lt;/p&gt; &lt;/div&gt; &lt;hr /&gt; {% endfor %} &lt;/div&gt; &lt;div class="forms"&gt; &lt;form action="." method="post" name="posts" id="posts"&gt;{% csrf_token %} &lt;table&gt; &lt;tr&gt;&lt;td&gt;{{form.author.label}}&lt;/td&gt;&lt;td&gt;{{form.author}}&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;{{form.title.label}}&lt;/td&gt;&lt;td&gt;{{form.title}}&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;{{form.bodytext.label}}&lt;/td&gt;&lt;td&gt;{{form.bodytext}}&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;&lt;input type="button" name="btnSave" id="bntSave" value="Submit" class = "default2"/&gt;&lt;/td&gt; &lt;/table&gt; &lt;/form&gt; &lt;/div&gt; &lt;/body&gt; </code></pre> <p><strong>settings.MIDDLEWARE_CLASSES</strong>:</p> <pre><code>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', ) </code></pre> <p>Please let me know if i am missing anything. Also let me know how can I achieve this without using a Django model form.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload