You can pass strings from URLs as arguments into your Django view functions.
Assuming your Jurisdiction model already has a "name" field, make sure it
also has a "postal_code" field (consider renaming the model State?)
Your urls and views can be something like:
# urls.py
url(r'^(?P<state_code>[A-Z]{2})', views.state_home, name="state_home"),
#views.py
def state_home(request, postal_code):
state = Jurisdiction.objects.get(postal_code=postal_code)
return render(request, 'mystatetemplate.html', {'state': state})
If you want to have several state-related pages then put the URLs in a
dedicated "states" app or similar and 'include' it from your top level URLs
file, eg. url(r'^states/', include('states.urls'))
On Friday, December 16, 2016 at 11:19:38 PM UTC, Malik Rumi wrote:
>
> I already got one fast and helpful answer today, so I’m going to be greedy
> and press my luck.
>
>
> I have this website. Each state has their own home/landing page off the
> site’s main page, and from there you will be able to get detail pages about
> various tidbits about the state of your choice. I have implemented this
> with a urlconf that looks for the state’s 2 digit postal name:
>
>
> url(r'^(?P<twodigit>[A-Z]{2})', include('bench.urls', namespace=twodigit
> )),
>
> It will come as no surprise that the views and templates associated with
> each state are identical. However, in order to be DRY, I wanted the view to
> take the twodigit argument from the url and call the right state’s
> queryset. To this end, I created a dict
> {'AK': 'Alaska',
> 'AL': 'Alabama',
> 'AR': 'Arkansas',
> ...etc…}
>
>
> naively thinking I would be able to do something like
>
>
> for k,v in statedict:
> if twodigit == k:
> state = Jurisdiction.objects.get(v)
>
>
> However, this does not work. I’m not sure why. Here are some of the
> various results I’ve gotten as I tried tweaking it:
> for k,v in statedict:
> if 'VA' == k: # I was thinking of this as almost a default value
> state = Jurisdiction.objects.get(v)
>
>
> However, this gets an unbound local error because of the scope, and I
> don’t know how to assign the variable so that it is accessible outside the
> scope of the for loop.
>
>
> k='NE'
> print(v)
> k=="US"
> print(v)
>
>
> returned
>
> U
> U
>
>
> Clearly, there is no ‘U’ in Nebraska, so I don’t know what happened there.
>
>
> This works
>
>
> print(statedict['US'])
> (aishah) malikarumi@Tetuoan2:~/Projects/aishah/jamf35$ python statedict.py
>
> United States
>
>
> But this does not
>
>
> File "statedict.py", line 63, in <module>
> if statedict['k']:
> KeyError: 'k'
>
>
> And this
>
>
> for k, v in statedict:
> if k:
> print('v')
>
>
> Gets me a ‘v’ for every state.
>
>
> Variations on
>
>
> Jurisdiction.objects.filter(statedict[’v']) and
> Jurisdiction.objects.filter(name='v’)
>
>
> also failed, and nothing I have found on the internet has helped. Ideas?
>
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/ab306de8-d4d7-4080-b5be-2904446dbb4f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.