I'm new to Python and Django, I've been through the tutorial that builds a blog, I've been through another tutorial that builds an page based on beers and breweries.
While going through this I started my own project while following along. 1st page lists a few states with links, that goes to the 2nd page that lists out specific cities within those states.
From there I want to link to a 3rd site specific items related to that city. the first two pages work great. however when I click the city name no matter what I've tried I get a blank page.
And I can not for the life of me figure out where I'm going wrong. I don't think I fully understand the flow of variables from the views.py to the urls.py to the html.
Maybe someone can give me a little guidance on where I'm going wrong. Again please be patient as I'm very new to Django and have a simplistic grasp of python.
class Meta(models.Model):
rcabbr = models.CharField(max_length = 15)
slug = models.SlugField(unique=False)
state = models.ForeignKey('State')
rc_state = models.CharField(max_length = 3)
oerp = models.CharField(max_length=18)
subgrp = models.SlugField()
sonus_pic = models.CharField(max_length=8)
ems = models.CharField(max_length=14)
agc = models.CharField(max_length=14)
def __unicode__(self):
return self.rcabbr
class State(models.Model):
name = models.CharField(max_length=2)
slug = models.SlugField(unique=True)
state_long = models.CharField(max_length=15)
owning_site = models.CharField(max_length=12)
def __unicode__(self):
return self.name
return self.state_long
and my views.py
def StateAll(request):
statelist = State.objects.all().order_by("name")
context = {'states':statelist}
return render_to_response('statelist.html',context,
context_instance=RequestContext(request))
def RcView(request, rclist):
rcs = Meta.objects.filter(rc_state = rclist)
context = {'rc_list': rcs}
return render_to_response('rclist.html',context, context_instance=RequestContext(request))
def RateCenterView(request,rcviews): # = rcviews is passed from the URL conf
rcv = Meta.objects.filter(rcabbr = rcviews)
context = {'whatrcv': rcv}
return render_to_response('rcview.html',context, context_instance=RequestContext(request))
and my URLS.py
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^$', TemplateView.as_view(template_name='index.html')),
url(r'^meta/$', 'meta.views.StateAll'),
(r'^meta/(?P<rclist>.*)/$', 'meta.views.RcView'),
(r'^meta/(?P<rcviews>.*)/$', 'meta.views.RateCenterView'),
)
and the snippets of code from page to page this lists out the states
{% for state in states %}
<p><a href="/meta/{{ state.name }}/">{{ state.state_long }}</a></p>
{% endfor %}
this takes the state and lists the cities, with some extra stuff in there just experimenting
{% for rc in rc_list %}
<p><a href="/meta/{{ rc.slug }}">{{ rc }}</a></p>
<p>RateCenter Slug: {{ rc.slug }}</p>
<p>RateCenter RC_State: {{ rc.rc_state }}</p>
<p>RateCenter: {{ rc }}</p>
{% endfor %}
this is the page thats broke
{% for rca in whatrcv %}
<p>RateCenter Slug: {{ rca.slug }}</p>
<p>RateCenter RC_State: {{ rca.rc_state }}</p>
<p>RateCenter: {{ rca }}</p>
{% endfor %}
The issue in your urls, your two urls are same and also in your html url(href="/meta/city/{{ rc.slug }}) not ending with /. change your url like this one:
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
(r'^$', TemplateView.as_view(template_name='index.html')),
url(r'^meta/$', 'meta.views.StateAll'),
(r'^meta/(?P<rclist>.*)/$', 'meta.views.RcView'),
(r'^meta/city/(?P<rcviews>.*)/$', 'meta.views.RateCenterView'),
)
Then change your html like this :
{% for rc in rc_list %}
<p><a href="/meta/city/{{ rc.slug }}/">{{ rc }}</a></p>
<p>RateCenter Slug: {{ rc.slug }}</p>
<p>RateCenter RC_State: {{ rc.rc_state }}</p>
<p>RateCenter: {{ rc }}</p>
{% endfor %}
This will work for you.