On Jan 31, 7:29 pm, Nick <nickt...@gmail.com> wrote: > Thanks in advance for anyone answering this thread. > > I am building a DB of local government representatives. My goal is to > spit out a general list of the reps and a detailed bio page for each > rep. > > The URL structure I would like to have is 'government/reps/list' for a > list of all of the Reps and 'government/reps/ > REPSLASTNAME_REPSFIRSTNAME' for the single bio view. > > Here is my model: > class Rep(models.model): > <There is a bunch of dictionaries of the choices that go right here > that I'm ommitting> > Type = models.CharField(max_length=100, choices=Type_Choices, > blank=True) > Last_Name = models.CharField('Last Name', max_length=100, > blank=True) > First_Name = models.CharField('First Name', max_length=100, > blank=True) > Position = models.CharField('Position', help_text="Only used for > non-council City and Local officials", max_length=100, blank=True, > choices=Position_Choices) > Party = models.CharField(max_length=3, choices=Party_Choices, > blank=True) > District = models.IntegerField(help_text="For State, Federal and > County Commissioners", blank=True, null=True) > Ward = models.IntegerField(help_text="For City Councils", > blank=True, null=True) > City = models.CharField(max_length=100, blank=True) > Phone = models.CharField(max_length=15, help_text="Use the form > xxx-xxx-xxxx", blank=True) > Email = models.EmailField(max_length=100, blank=True) > Contact_URL = models.URLField(max_length=200, blank=True) > DOB = models.DateField('Date of Birth',blank=True, null=True) > Gender = models.CharField(blank=True, max_length=2, > choices=Gender_Choices) > Begin_Serve = models.IntegerField('Began Serving in', blank=True, > null=True) > End_Serve = models.IntegerField('Term Limited in', blank=True, > null=True) > MugShot = models.ImageField('Mug Shot Upload', help_text="images > are to be no larger that 150x200", storage=s3_storage, > upload_to='newsok/images/Government/images/mugs', height_field=None, > width_field=None, max_length=300, blank=True) > Committees = models.ManyToManyField('Committees', blank=True) > Approp_Committee = models.ManyToManyField('Approp_Committees', > blank=True) > > def __unicode__(self): > return u"%s, %s" % (self.Last_Name, self.First_Name) > > class Meta: > abstract = False > > Here is my view: > > from Government.Reps.models import * > from django.shortcuts import render_to_response > from django.http import Http404 > > def index(request): > rep_list_view = Rep.objects.all().order_by('Last_Name') > return render_to_response('Government/repsAll.html', {'allReps': > rep_list_view}) > > def detail(request, (Last_Name, First_Name)): > try: > r = Rep.objects.all().order_by('Last_Name', 'First_Name') > except Rep.DoesNotExist: > raise Http404 > return render_to_response('Government/repsSingle.html', > {'singleRep': r}) > > Here is my URL conf: > > from django.conf.urls.defaults import * > > # Uncomment the next two lines to enable the admin: > from django.contrib import admin > admin.autodiscover() > > urlpatterns = patterns('', > # Example: > # (r'^Government/', include('Government.foo.urls')), > > # Uncomment the admin/doc line below and add > 'django.contrib.admindocs' > # to INSTALLED_APPS to enable admin documentation: > # (r'^admin/doc/', include('django.contrib.admindocs.urls')), > > # Uncomment the next line to enable the admin: > (r'^admin/', include(admin.site.urls)), > (r'^reps/(P?<Last_Name>_<First_Name>\d+)/$', > 'Government.Reps.views.detail'), > > I think that I am either overthinking this or way UNDER thinking it. > > This is my first go at a template being called via variables passed in > the URL. I have a good background in templates and the models portion > of django but am sorely lacking in my understanding of views and URL > confs. > > Thanks again, > Nick
You're on the right track. A couple of things though. Firstly, your urlconf. You need to learn a bit about regular expressions: \d+ matches one or more numeric digits, which obviously isn't what you want. And you have the P? the wrong way round - it should be ?P. Also, you want first name and last name to be sent as separate variables, so you want one (?P ) group per variable. So what you need is this: (r'^reps/(?P<last_name>\w+)_(?P<first_name>\w+)/$', 'Government.Reps.views.detail'), Although note that this won't match names like O'Connell, as \w doesn't match an apostrophe. Perhaps this is better: (r'^reps/(?P<last_name>[A-Za-z']+)_(?P<first_name>[A-Za-z]+)/$', 'Government.Reps.views.detail'), Secondly, in your view, you have some extra brackets in the function definition: def detail(request, last_name, first_name): And you want to use .get() to get the actual matching Rep: r = Rep.objects.get(Last_Name=last_name, First_Name=first_name) Note that this will raise an exception if a matching Rep is not found. You may want to make this show a 404 page if that happens, in which case you can use the handy get_object_or_404 shortcut: from django.shortcuts import get_object_or_404 r = get_object_or_404(Rep, Last_Name=last_name, First_Name=first_name) Finally, although this isn't really an error, you should take note of PEP8 to use the Pythonic style of capitalisation. Class attributes (eg field names) and normal variables are usually spelled all lower case, which is why I've used last_name and first_name above. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.