anshp
anshp

Reputation: 31

Form fields not being displayed Django

I am still learning Django. I have been looking at various tutorials but I am struggling with forms on django framework. The problem is that I am unable to display the form field for email on the base.html page. It is basically a subscription form. I am still new to django. This is what I have made till now.

models.py

from django.db import models
class SubscribeModel(models.Model):
    email = models.CharField(max_length=100)

forms.py

from django import forms
from models import SubscribeModel

class SubscribeForm(forms.ModelForm):

    class Meta:
        model=SubscribeModel

views.py

def loadform(request):
    form = SubscribeForm()
    return render_to_response('base.html', {'form': form},context_instance=RequestContext(request))

base.html

<html>
 ...
 <form action="" method="post">{% csrf_token %}
{{ form.as_p }}
   <input type="submit" value="Subscribe"></ul>
 </form>
 ...
</html>

urls.py

urlpatterns = patterns('',
url(r'^$', 'chsite.blog.views.index'),
url(r'^$', 'chsite.blog.views.loadform'),
url(r'^admin/', include(admin.site.urls)),
)

Upvotes: 0

Views: 63

Answers (2)

timo.rieber
timo.rieber

Reputation: 3867

You have to define different url patterns as your first two are identical. URL processing stops at first hit, in your case uses index view.

Upvotes: 1

slackmart
slackmart

Reputation: 4934

Once you are edited the question, I can see two url(---) reffenceced to the same url. Hence Django will loadd the first one. It means you never load the loadform view. So try removing the line:

url(r'^$', 'chsite.blog.views.index'),

or edit the actual tuple for the loadform for instance:

url(r'^suscribe/$', 'chsite.blog.views.loadform'),

and go to your browser at 127.0.0.1:8000/suscribe/ url.

Upvotes: 1

Related Questions