I’m currently trying to learn Django from the book and I read most of it and am now trying to write a webapp of my own. I just really can’t figure out how they all interact and how they aren’t fitting together.
My urls.py file reads like this…
from django.conf.urls.defaults import patterns, include, url
from users.views import homepageview, submitted, referral_create
urlpatterns = patterns('',
(r'^$', homepageview),
(r'^submitted/$', referral_create),
the views.py file looks like this…
# Create your views here.
from django.shortcuts import render_to_response
from django import forms
from datreferral.users.forms import ReferralCode
def homepageview(request):
now = 'your damn referral code'
return render_to_response('datreferraltemplate.html', {'now': now})
def referral_create(request):
if request.method == 'POST':
form = ReferralCode(request.POST)
if form.is_valid():
new_code = form.save()
return HttpResponseRedirect(reverse(contact_details, args=(new_contact.pk,)))
else:
form = ReferralCode()
The form.py file looks like
from django import forms
class ReferralCode(forms.Form):
referralcode = forms.CharField()
and the template looks like this…
{% extends "base.html" %}
{% block title %}
Enter your referral codes!
{% endblock %}
{% block content %}
<h1>Enter your code</h1>
{% if errors %}
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endif %}
<form action="" method="POST">
<p>Subject: <input type="text" name="subject" value="{{ referralcode }}"></p>
<input type="submit" value="Submit">
</form>
{% endblock %}
Hopefully that’s enough info.
I am looking for two things. First off, when I try to view the page after submitting the form i get no where because I assume that the “if request.method == ‘POST’:” is not triggering. Obviously its something pretty obvious but I’m in one of those modes where I can find the bug for the life of me.
The second question I have is basically is plea for help. somehow after reading through those chapters multiple times I can’t seem to nail down how all of the pieces interact with each other. I know that the template and the urls.py and the views.py interact and I get (I think) but I can’t really grasp how the database and forms interact with each other and with the views/templates. like say I just wanted to a simple form where whatever the use inputted is written into the database… How would you do that? I’m using postgres if that matters.
Note: the form and the in the template is a modified version of code i found on here and tried to manipulate to meet my needs but failed so don’t be overly thrown off if that doesn’t make sense I wasn’t able to mess with that part that much yet because of these problems.
As I’m new to web development I really appreciate any one willing to help me or point me in the right direction.
You don’t return anything in you else clause. A views must always return a response, which can be pretty much anything, but in most cases you will return a instance of a (sub)-class of
HttpResponse(I really like therendershortcut). It is a good idea to have a default return at the bottom of you view, add some early returns for “special” responses and otherwise let the execution reach the default return – this way you never have the case where you return nothing.You have to use a Model to save data (have you work through the tutorial?). Usually the excution model is the following:
Thats it. This is a bit simplified but it pretty much covers all important parts.