My "Contributors" page Conundrum

51 views
Skip to first unread message

JJ Zolper

unread,
Aug 28, 2012, 12:58:37 AM8/28/12
to django...@googlegroups.com
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

Nick Santos

unread,
Aug 28, 2012, 1:49:08 AM8/28/12
to django...@googlegroups.com
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


--
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...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Melvyn Sopacua

unread,
Aug 28, 2012, 2:23:32 AM8/28/12
to django...@googlegroups.com
On 28-8-2012 6:58, JJ Zolper wrote:

> 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.

<https://docs.djangoproject.com/en/1.4/ref/models/instances/#django.db.models.Model.get_absolute_url>
and make sure you read the permalink bit.
--
Melvyn Sopacua

JJ Zolper

unread,
Sep 19, 2012, 1:28:05 AM9/19/12
to django...@googlegroups.com, ni...@enviroconsumer.org
Nick,

Sorry my other very long posted vanished so I'm going to keep it shorter this time.

So I was able to implement what you said. Thanks a lot!

Only tricky part is I had to change my name in the database to "JJZolper" instead of "JJ Zolper" so that it could find me.

So now if you do:


It works!

However, that's not very practical so I could use some help still. I'm wondering if I should come up with some other field in my contributor database like "id" or something and see if I could make it so it takes the name "JJ Zolper" and creates an id of "jjzolper" and thus when 


is called everything is happy because the same code you showed me would go through the database and work fine! Is this a good practice or is there a better way?

Also, even though I've made progress on requesting the page with the name and all that I still have another problem.

On this page:


If you click a name to hopefully go to the page of that contributor it breaks. Here is the code:

<ol>
{% for Contributor in Contributors_List %}
   <li><a href="{{ Contributor.get_absolute_url }}">{{ Contributor.name }}</a><li>
<ul>
<li>Title: {{ Contributor.title }}</li>
</ul>
{% endfor %}
</ol>

So bascially it pulls the contributor name which is messed up now cause I changed my name to JJZolper but this seems to work.

I was trying to go after what Melvyn said about permalink and get absolute url to try to solve the problem of making these two pages actually connect up but I still haven't made it work. Any ideas there?

I could use some help basically depending on the object handling the absolute url function and then sending it off to the respective url and then the file. I mean those are the right steps I think?

Thanks so much,

JJ

PS. I'm making sure to copy this so hopefully it won't just crap out on me before I post it. Oh the joys of spending time on something and it getting destroyed. The internet is fun, eh? haha

JJ Zolper

unread,
Sep 19, 2012, 1:30:41 AM9/19/12
to django...@googlegroups.com
Melyvn,

Thanks for the tip but I'm still having trouble understand how to make this work.

If you see the post I sent to Nick that should explain my problem in enough detail.

Maybe you can give some advice or any example of some sort?

Thanks,

JJ

JJ Zolper

unread,
Sep 20, 2012, 8:28:57 PM9/20/12
to django...@googlegroups.com
Anyone have any ideas?

Thanks!

JJ Zolper


On Tuesday, August 28, 2012 2:24:03 AM UTC-4, Melvyn Sopacua wrote:

Thomas Lockhart

unread,
Sep 20, 2012, 8:53:25 PM9/20/12
to django...@googlegroups.com
On 9/20/12 5:28 PM, JJ Zolper wrote:
Anyone have any ideas?
Yes, Melvyn did.

hth

                          - Tom

--
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/-/uFK1RTmzm5MJ.

JJ Zolper

unread,
Sep 21, 2012, 11:03:04 PM9/21/12
to django...@googlegroups.com
Thanks Thomas.

Now does anyone have any legitimate help? Because I'm stuck.

Thanks,

JJ

JJ Zolper

unread,
Sep 22, 2012, 5:38:05 PM9/22/12
to django...@googlegroups.com, ni...@enviroconsumer.org
Okay Nick so your help really made a big difference. The generic view and urlconf helped a lot.

Well I didn't figure out how to do Melvyn's recommendation but I was able to implement this another way. It's good but not perfect.

Here's all the relevant files:

- about models.py

from django.db import models
from django.forms import ModelForm

class Contributor(models.Model):
    name = models.CharField(max_length=30)
    lowername = models.CharField(max_length=30)
    title = models.CharField(max_length=60)
    bio = models.CharField(max_length=5000)
    website = models.URLField()

    def __unicode__(self):
       return self.name

class ContributorForm(ModelForm):
    class Meta:
        model = Contributor

Basically, my key was to add another field called lowername. This field would hold the lowercase version of the contributors name. So for me it is "jjzolper". By doing this with your help I was able to make it happen. And continuing on:

- about contributors (the page that lists all contributors so pulls it out of the database:

{% extends "base.html" %}

{% block HTMLTitle %}<title>About</title>{% endblock %}

{% block CSSFiles %}
<!-- Standard CSS Files -->
<!-- Standard CSS Files -->
{% endblock %}

{% block JSFiles %}
<!-- Standard JavaScript Files -->

<!-- Standard JavaScript Files -->
{% endblock %}

{% block content %}
<div id="content">

<div id="main">

<center><u>Contributors</u></center>

<ol>
{% for Contributor in Contributors_List %}
   <li><a href="http://www.madtrak.com/about/contributors/{{ Contributor.lowername }}">{{ Contributor.name }}</a><li>
<ul>
<li>Title: {{ Contributor.title }}</li>
</ul>
{% endfor %}
</ol>

</div>
<div class="clear"></div>
</div>
{% endblock %}

After having created this new field in the database I could call that field as the relevant URL! The second key.

Then it was simple from there on out with your code.

the urlconf you described:

- urls.py

(r'^about/contributors/(?P<contributorname>[a-zA-Z]+)$', 'madtrak.about.views.contributor'),

That value like you said is passed through this thing. It is sent to the view for a singular contributor:

- about views.py

def contributor(request, contributorname):
    contributor_object = Contributor.objects.filter(lowername = contributorname)
    return render_to_response('contributor.html', {'Contributor': contributor_object}, context_instance = RequestContext(request))

It tests to see based on the url for the request which entry corresponds to it.

about contributor.html (singular so the relevant contributor)

{% extends "base.html" %}

{% block HTMLTitle %}<title>About {{ Contributor.name }}</title>{% endblock %}

{% block CSSFiles %}
<!-- Standard CSS Files -->
<!-- Standard CSS Files -->
{% endblock %}

{% block JSFiles %}
<!-- Standard JavaScript Files -->

<!-- Standard JavaScript Files -->
{% endblock %}

{% block content %}
<div id="content">

<div id="main">

<ol>
{% for Contributor in Contributor %}
<ul>
<li>Name: {{ Contributor.name }}</li>
<li>Title: {{ Contributor.title }}</li>
<li>Bio: {{ Contributor.bio }}</li>
<li>Website: {{ Contributor.website }}</li>
</ul>
{% endfor %}
</ol>

</div>
<div class="clear"></div>
</div>
{% endblock %}

Thanks again for the great advice. Currently this is my best implementation of my about contributor page. I really appreciate your kind words and help when doing this.

If there is a better way I'm all ears! But this is what I'm going with for the time being!

Thanks so much,

JJ


On Tuesday, August 28, 2012 1:50:09 AM UTC-4, Nick Santos wrote:
Reply all
Reply to author
Forward
0 new messages