Hi JJ,

You're absolutely right that there is a better way to do this that doesn't
involve repetition. To start with, check out the docs under example on the
page for the URL dispatcher:
https://docs.djangoproject.com/en/dev/topics/http/urls/ - I'll walk you
through part of it though.

First, let's take a look at how capture groups work. Capture groups allow
you to pass a variable portion of the url to a view, which is what you'll
need to do in order to have one definition that lets you have a generic
view that looks up the contributor. So, you can assign a view to a URL
where only part of it is known at the time of the definition, and pass the
unknown parts into the view. In your case, your url definition would look
like:

urlpatterns = patterns('',
    .... your other patterns...
    (r'^about/contributor/(?P<contribname>[a-zA-Z]+)/$', 'your.view.name'),
    ....possibly more patterns ....
)

So, what that (?P<contribname>[a-zA-Z]+) says, in parts is that we want to
capture a value - designated by the parenthesis - to be passed to
your.view.name as a named parameter called contribname - this is defined by
the ?P<contribname>. That value looks like text with at least one
character. The text definition is [a-zA-Z] (careful, this doesn't include
spaces right now)and the at least one is +, and comes between two slashes.
If you want to learn more about writing things like that, look into regular
expressions.

Then, in your view, you can take that parameter and look up the relevant
contributor and make the view generic to something like:

def contributor_page(request, contribname):
    contrib_object = Contributor.objects.filter(name=contribname)
    return render_to_response('contributor.html', {'Contributor':
contrib_object}, context_instance = RequestContext(request))

Then, in outputting your links, you can put the relevant name in the url,
etc.

I hope that helps. Let me know if anything is unclear. Good luck


On Mon, Aug 27, 2012 at 9:58 PM, JJ Zolper <codinga...@gmail.com> wrote:

> Hello,
>
> I'm trying to develop a simple hyperlink between two pages. It sounds
> simple but it's a little bit more complex then that.
>
> Here is the template code that proceeds through the database of
> contributors:
>
> <center><u>Contributors</u></center>
>
> <ol>
>  {% for Contributor in Contributors_List %}
>     <li><a href="http://www.madtrak.com/about/contributors/";>{{
> Contributor.name }}</a></li>
>  <ul>
> <li>Title: {{ Contributor.title }}</li>
> </ul>
>  {% endfor %}
> </ol>
>
> and spits out the contributors name in a link form and the title of that
> person.
>
> My problem is that I want each contributor to have their own separate
> page. So if the first guys name for some example is Mike Smith then if you
> were to click his name for example you would be sent to
> /about/contributor/mikesmith and so on. I supposed I could define a url for
> each contributor so I could set this up:
>
> <center><u>Contributors</u></center>
>
> <ol>
> {% for Contributor in Contributors_List %}
>      <li><a href="{{ Contributor.link">{{ Contributor.name }}</a></li>
> <ul>
>  <li>Title: {{ Contributor.title }}</li>
> </ul>
> {% endfor %}
>  </ol>
>
> but that doesn't seem like the correct way to do this. that
> Contributor.link is then hardcoded into the system. It's not generated by
> the system obviously.
>
> I also have:
>
> def mikesmith(request):
>     mikesmith = Contributor.objects.filter(name='Mike Smith')
>     return render_to_response('mikesmith.html',
> {'Contributor': mikesmith}, context_instance = RequestContext(request))
>
> I have that repeated for each and every contributor. This goes againist
> Django's DRY mentality so I have a feeling there is a much better way.
>
> Thanks,
>
>  JJ
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/xWx39cCFzvYJ.
> To post to this group, send email to django-users@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.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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.

Reply via email to