regex - Regular expressions based off model entry in Django? -
if create new user in database modeled following:
if question doesn't make sense, i'm more wondering data exists in example url like: url(r'^members/(?p<username>\w+)/$', 'profiles.views.single')
what , how can create field that?
from django.db import models django.contrib.auth.models import user class address(models.model): user = models.foreignkey(user) street_address = models.charfield(max_length = 200) city = models.charfield(max_length = 100) state = models.charfield(max_length = 100) zipcode = models.integerfield(max_length = 5) updated = models.datetimefield(auto_now = true, auto_now_add = false) timestamp = models.datetimefield(auto_now = false, auto_now_add = true) active = models.booleanfield(default = true) def __str__(self,): return (self.user)
how possible have regular expression url mapped based on user lies within model? possible? or maybe phonenumber if had phone number field in there. curious, trying better understand regex. thanks!
i want
def single(request, username1): try: user = address.objects.get(address.user=username1) single_user = user except: raise http404 return render_to_response('single_user.html', locals(), context_instance=requestcontext(request))
to work with: url(r'^members/(?p<username1>\w+)/$', 'profiles.views.single')
the thing wrong example code give how perform query: don't use class name inside parentheses.
user = address.objects.get(user=username1)
if didn't have catch-all except clause there, have received syntaxerror might have helped debug yourself: that's why it's very bad idea have those. catch address.doesnotexist
exception, or better use get_object_or_404
shortcut.
Comments
Post a Comment