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:
>
> 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 <codin...@gmail.com<javascript:>
> > 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...@googlegroups.com<javascript:>
>> .
>> To unsubscribe from this group, send email to 
>> django-users...@googlegroups.com <javascript:>.
>> 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 view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/smTxt6Qz240J.
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