render a models slug field as a url

103 views
Skip to first unread message

Matthew Meyer

unread,
Jul 24, 2012, 9:42:45 PM7/24/12
to django...@googlegroups.com
Hello all,

I'm trying to load the url of a model created with get_absolute_url() programmatically in my view. There are basically three steps I need to accomplish this:

1. make sure @permalink is working correctly. This step I am fairly confident I have completed. Here is the model I use: http://dpaste.org/nH0cZ/

2. make sure I am rendering the url correctly in my view: http://dpaste.org/FOGjs/ 

This view is fairly involved. The return statement on line 12 is where I am trying to load the url. In a nutshell, if the request is GET this view renders a form for the user which they can use to fill some fields in a model. one of the fields they fill ("name") I want to use as my the rendered url, which is why I am trying to pass {'event': Event} on line 12 when request.method == 'POST' and the user has submitted the form. Right now when the user submits the post, the event_page.hml loads but without the event object url.

3. make sure my urlconfs are working:: http://dpaste.org/LPdfK/ Not positive here, but I know this is fairly simple to most. I just tried to follow what the docs had for get_absolute_url and @permalink (https://docs.djangoproject.com/en/dev/ref/models/instances/?from=olddocs#django.db.models.permalink) Something is wrong though because the "view on website" button in admin is leading to a permalinked url that doesn't match.

As you can see, I am quite a bit lost and could really use some guidance. Any help is appreciated. Thanks!

Daniel Roseman

unread,
Jul 25, 2012, 6:29:42 AM7/25/12
to django...@googlegroups.com
Your problem is in step 2. `Event` is the model class itself, but you want to render the URL for a particular instance of that class. Presumably you want the event that has been created by the form. I don't understand why you are using a formset though: that implies you are creating multiple events, whereas you talk about showing the URL for a single event, which makes more sense. Sounds like you should use a simple ModelForm, and if it's valid do `event = form.save()` - now you can pass that `event` object to the template.

Finally, note that rendering a template directly after a POST is a bad idea, as the documentation explains. You should always redirect to a new page: you could redirect directly to the new event's URL if you like.
--
DR.

Matthew Meyer

unread,
Jul 25, 2012, 10:45:41 PM7/25/12
to django...@googlegroups.com
HI daniel, thanks for the reply

After your comments, I have used ModelForm to simplify my code. I have also achieved redirecting the desired url by using the redirect() function. My issues are still basically the same however:

- I do not know how to tell Django to render a template at the redirected url.

I've tried passing redirect differnt views and params etc. to no avail.

Here is my new views.py: http://dpaste.org/EtxdP/

I am trying to have the event_page view handle the redirect and render the template it returns. I know I am not relating the redirect and the view at all, this is because I do not know how. If I try to relate them in some way like this: return redirect('event_page', event)

I get the error: Reverse for 'event_page' with arguments '(<Event: pppzzz>,)' and keyword arguments '{}' not found.

My urls.py looks like this now: http://dpaste.org/UzBcR/ 

I am sort of hazy on the use of the third parameter in my second url pattern. I know the first param is a regex, the second is the view that handles the url, and from what I understand by reading the docs, the third param is a type of distinguisher used if you are calling the same view for different urls. 

Any thoughts on where I would begin to for rendering a template at the redirected url?

Thanks,
Matt

Daniel Roseman

unread,
Jul 26, 2012, 4:25:10 AM7/26/12
to django...@googlegroups.com
On Thursday, 26 July 2012 03:45:41 UTC+1, Matthew Meyer wrote:
HI daniel, thanks for the reply

After your comments, I have used ModelForm to simplify my code. I have also achieved redirecting the desired url by using the redirect() function. My issues are still basically the same however:

- I do not know how to tell Django to render a template at the redirected url.

I've tried passing redirect differnt views and params etc. to no avail.

Here is my new views.py: http://dpaste.org/EtxdP/

I am trying to have the event_page view handle the redirect and render the template it returns. I know I am not relating the redirect and the view at all, this is because I do not know how. If I try to relate them in some way like this: return redirect('event_page', event)

I get the error: Reverse for 'event_page' with arguments '(<Event: pppzzz>,)' and keyword arguments '{}' not found.

My urls.py looks like this now: http://dpaste.org/UzBcR/ 

I am sort of hazy on the use of the third parameter in my second url pattern. I know the first param is a regex, the second is the view that handles the url, and from what I understand by reading the docs, the third param is a type of distinguisher used if you are calling the same view for different urls. 

Any thoughts on where I would begin to for rendering a template at the redirected url?

Thanks,
Matt


You haven't quite understood what a redirect is. A redirect doesn't render a template: a redirect is just a signal to the browser to go and fetch another, completely different, URL. Naturally, Django will then reply to that second request with the template rendered by the view pointed to by that second URL.

The third parameter to the url pattern is basically a name Django can use to refer to that URL, so that it can 'reverse' it and produce the actual URL for use in things like links, redirects and permalink definitions.

So, the main problem now is that you probably want the absolute URL to be the `event_page` view. You need to update the get_absolute_url method to point to that URL - as I said above, you can use the short name 'event_url' rather than the fully qualified view name. However, you also need to update the event_page view to accept the `name` parameter, and then to actually get the relevant Event object from that name - eg `event = Event.objects.get(name=name)` - and pass it to the event_page.html template.
--
DR.

Matthew Meyer

unread,
Jul 26, 2012, 7:30:10 AM7/26/12
to django...@googlegroups.com
That was it, it is working now. Thank you so much Daniel! :)
Reply all
Reply to author
Forward
0 new messages