Django URL Patterns - python

I've created a very simple blog, but have been running into a couple URL issues. For my tag & specific post views I run into the following issues.
Specific Post View ExampleThese two sites render the same, and would like the second one to render a 404. website.com/post/1/hello-world website.com/post/1/hello-world/non-sense (should render 404)
Tag View
website.com/tag/python: this will render all posts tagged python, great. However...
website.com/tag/python/party: this will render all posts tagged "python/party" instead of rendering a 404.
Here is my URL patterns setup so you can take a look.
url(r'^post/(?P<pk>\d+)/(?P<post_title>)', DetailView.as_view(
model = post,
template_name = "post.html")),
url(r'^post/$', ListView.as_view(
queryset = post.objects.all().order_by("-date"),
template_name = "archives.html")),
url(r'^archives/$', ListView.as_view(
queryset = post.objects.all().order_by("-date"),
template_name = "archives.html")),
url(r'^tag/(?P<tag>[\w|\W]+)', 'tags'),
Updated
Solution for tag:
url(r'^tag/(?P<tag>[\w]+)\W*/$', 'tags'),
Solution for post:
url(r'^post/(?P<pk>\d+)/(?P<post_url>[\w-]+)/$', DetailView.as_view(
model = post,
template_name = "post.html")),
Thank you Huckleberry Finn and krakax for all the help!

Your post URLconf regex
url(r'^post/(?P<pk>\d+)/(?P<post_title>)', DetailView.as_view(
model = post,
template_name = "post.html")),
Should changed to
url(r'^post/(?P<pk>\d+)/(?P<post_title>[-\w]+)/$', DetailView.as_view(
model = post,
template_name = "post.html")),
means URLconf is ending with end-slash
Anyway, Try to define you DetailView URLconf after post ListView. In my opinion if you change your list view and detailview to posts/ and post/ you problem will be solved. The solution is same for tags URLconf issue.

Your regex
r'^tag/(?P<tag>[\w|\W]+)'
means that group tag will contain all caracters after 'tag/'. [\w|\W] means 'all alphanumerics' or 'all but alphanumerics'. This is equivalent to 'all caracters'.
It should be changed to
r'^tag/(?P<tag>[\w]+)\W*'
This will stop your group at first non alphanumeric

Sure, right now I see 2 ways.
First, change to:
r'^tag/(?P<tag>\w+)(?P<end>\W.*)'
(BTW, [] is not necessary in your case) This way, you get the group called 'end' as a parameter of your tag controller and you can test it and redirect to your 404 custom page or generate a 404 classic error.
Second possibility is:
Add another url hook and controller:
url(r'^tag/(?P<tag>\w+)\W*', 'tags404'),
url(r'^tag/(?P<tag>\w+)$', 'tags'),
This way, you forbid any url that contains others caracters than alphanumerics after the 'tag/'.
If you want to allow just one optional '/' at the end, you can write in place of your new hook:
url(r'^tag/(?P<tag>\w+)/?$', 'tags'),

Related

Django Rest Framework with either a slug or a pk lookup field for the DetailAPIView

I have an api list of posts with Django Rest Framework. Knowing that I slugify my post title, whenever the user provides a title to her post, I want to capture the post api detail with the pk (the default lookup field in DRF) when the slug object is not provided, or using the slug field if the post has a title.
In my api folder of the post application, I wrote the following views.py:
class PostDetailAPIView(generics.RetrieveAPIView):
queryset = Post.objects.all()
serializer_class = serializers.PostDetailSerializer
try:
lookup_field = 'slug'
except:
lookup_field = 'pk'
And provided the corresponding urls.py:
urlpatterns = [
url(r'^$', PostListAPIView.as_view(), name='list-api'),
url(r'^(?P<slug>[\w-]+)/$', PostDetailAPIView.as_view(),
name='detail-api'),
url(r'^(?P<pk>\d+)/$', PostDetailAPIView.as_view(), name='detail-
api'),
]
This approach works perfectly when a post has a slugified (title) lookup. But the post detail view for a post with no slug shows the following:
HTTP 404 Not Found
Allow: GET, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept
{
"detail": "Not found."
}
Any suggestions on how to show the post api detail for either a pk or a slug fields lookup? Preferably with no code repitition in the urls.py.
First: Your urls.py works with the first matched url. So it can not find your second url.
Second: In my opinion you should either stick to the "pk" Version (which is what REST states) or the slug version for the lookup. So in general i would anticipate the process of either looking for a pk or a slug.
I know this does not yet solve your answer to 100% but it should provide you a overlook on what's going on and what a REST Endpoint might look like.
Using two different parameter for a single view seems to be like a bad practice to me, i will suggest you use one of them and in your view, you do a try/catch since pk will be of integer type and title will be of string type, you can check the type of the url parameter then do your logic

Django admin - add custom link to change list view

Firstly thanks for taking the time to read my problem
I tried to add a custom button to my admin changeList view, but it always gives me Page not found (404)
This my modelAdmin code:
class MyModelAdmin(admin.ModelAdmin):
class Media:
js = ('admin/js/additional_js.js',)
model = MyModel
def get_urls(self):
urls = super(MyModelAdmin, self).get_urls()
analyze_url = patterns('',
(r'^(?P<pk>\d+)/analyze/$',
self.admin_site.admin_view(self.analyze_view))
)
return analyze_url + urls
def analyze_view(self, request, pk):
# some code here
HttpResponseRedirect(
reverse('admin:myapp_MyModel_change', args=(pk,))
)
My jQuery code for adding custom link to change list view:
(function($) {
$(document).ready(function($) {
$(".object-tools").append('<li>Analyze</li>');
});
})(django.jQuery);
when I click my custom link it gives me this:
MyModel object with primary key '3/change/analyze' does not exist.
I see that the link does not point to the view
Can someone help me fix this issue.
Thanks
In Django 1.9, the admin change url has changed to /admin/<app>/<model>/<pk>/change/ (release notes).
Therefore, it doesn't make sense to link to href="analyze/" in your html. The relative url is treated as /admin/<app>/<model>/3/change/analyze, not /admin/<app>/<model>/3/analyze as you expect.
You can probably fix the problem by changing the link to:
<a href="../analyze/" ...
Ideally, it would be better to reverse the url rather than having a relative link. I think this would make the code less fragile.
Since you are using Django 1.9, you can update your code to use a list rather than patterns:
from django.conf.urls import url
analyze_url = [
url(r'^(?P<pk>\d+)/analyze/$', self.admin_site.admin_view(self.analyze_view)),
]

How do I use DateDetailView with a slug to render a page with a single article?

I am new to Django, and I am trying to have a separate page where I can view individual articles. Currently I have:
#views.py
class ArticleView(DateDetailView):
template_name = 'blog/article.html'
model = Article
date_field = "pub_date"
#I am not sure which one to use
slug_field = "unique_url_suffix"
slug_url_kwarg = 'unique_url_suffix'
and
#urls.py
urlpatterns = [
url(r'^(index\.html)?$',views.IndexView.as_view(),name='index'),
url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<slug>[-\w]+)/$',
views.ArticleView.as_view(),
name="article_detail"),
]
and in index.html inside a loop of objects from the Article class:
<h2>{{article.title}}</h2>
I have also tried manually inputting the arguments, like this:
<h2>{{article.title}}</h2>
I keep on getting a "NoReverseMatch at /blog/" error. What am I doing incorrectly?
Edit: On top of the changes recommended for the answer, there was a typo causing problems. It does not affect the answer below, though.
First off, you should not be generating this URL in your template. You should define a get_absolute_url method in your Article model that looks like this:
from django.core.urlresolvers import reverse
def get_absolute_url(self):
# Note - you have to supply each of the date components separately
# because you need to match the URL regex.
return reverse (
'blog:article_detail',
kwargs={'year': self.pub_date.strftime("%Y"), 'month': self.pub_date.strftime("%b"),
'day': self.pub_date.strftime("%d"), 'slug': self.unique_url_suffix}
)
And then in your template:
<h2>{{article.title}}</h2>

Redirection is leading me to Page not found error in Django

Project urls.py includes app urls. I am using HttpResponseRedirect to get Likes posted on site. I am not trying to call for template so this is why not using render_to_response. My app view is:
def like_article(request, article_id):
if article_id:
a = Article.objects.get(id=article_id)
count = a.likes
count += 1
a.likes = count
a.save()
return HttpResponseRedirect('articles/get/%s' % article_id)
My app urls.py reflects likes redirection like this:
url(r'^like/(?P<article_id>\d+)/$', 'article.views.like_article'),
My parent "articles" HTML file extended from base says:
<p>{{article.likes}} people liked this article</p>
My single article page extended from base.html shows:
<p>Like</p>
Please advise.
You'd better use {% url [name] [parameters] %} in your template while reverse function in your view to create urls.
In your question, I think the problem is the url router doesn't match.
See:
<p>Like</p>
And:
url(r'^like/(?P<article_id>\d+)/$', 'article.views.like_article'),
It seemed the /article prefix doesn't appeared in you url.
Have you mapped the url - articles/get/article_id, i.,e added a similar pattern in urlpatterns (ex: url(r'^get/(?P<article_id>\d+)/$', 'article.views.get_article', name='get_article'),) tuple, to which you redirected the users!
If yes, then have you created a proper view for it!

passing data between class based forms

I am fairly new to Django and class based forms, and I am having trouble understanding how these interact with each other. Following from the django project example, I have tried to build a "search form", which would sit on all pages of my project:
# forms.py
from django import forms
class SearchForm(forms.Form):
myquery = forms.CharField(max_length=255,label="", help_text="sq")
def __unicode__(self):
return self.myquery
# views.py
from searchapp.forms import SearchForm
from django.views.generic.edit import FormView
from django.views.generic import TemplateView
class SearchView(FormView):
template_name = 'index.html'
form_class = SearchForm
success_url = '/searchres/'
def form_valid(self, form):
thequery=form.cleaned_data.get('myquery')
return super(SearchView, self).form_valid(form)
class Meta:
abstract = True
class SearchResView(SearchView):
template_name = 'searchres.html'
#urls.py
from django.conf.urls import patterns, include, url
from django.conf import settings
from deals.views import IndexView
from searchapp.views import SearchView, SearchResView
urlpatterns = patterns('',
url(r'^index/', SearchView.as_view(),name="home"),
url(r'^searchres/', SearchResView.as_view(),name="searchresx"),
)
The plan is the start off with a simple form for user to enter the search query, and also show the input form on the results page. I have the following questions here (sorry - I am a Django newbie esp. to Class Based Views):
How does one pass data ("thequery") to the success_url? i.e I would like success_url to have access to "thequery" so that I can use something like {{thequery}} on my template tags.
Upon submitting the form(name="home"), I see POST data from the form on my firebug, but I am able to see just "myquery" rather than "thequery". How does one use get_context_data() here to add/post "thequery" variable aswell?
Finally, I was wondering if it would be possible to construct the success_url based on "thequery" string i.e something like success_url = '/searchres/?q=' + thequery
Thank you in advance - I am hoping to learn more.
I would suggest using function based views for this. If you choose to subclass a generic view you will need to dig through a lot of documentation and possibly source code, to find the right methods to override. (If you're really keen then look at the ListView class along with the get_queryset(), get() and post() methods)
A single django view will normally handle both rendering the empty form AND processing the submitted form.
So the search page (both the form and the results), live at http://your-site.com/search. Your url conf is -
urlpatterns = patterns('',
#...
(r'^search/$', 'searchapp.views.search'),
)
And your view looks something like this -
def search(request):
if request.method == 'POST':
form = SearchForm(request.POST)
if form.is_valid():
my_query = form.cleaned_data['myquery']
object_list = YourModel.objects.filter(# some operation involving my_query)
return render_to_response('search_results.html', {'object_list': object_list})
else:
form = SearchForm()
render_to_response('search_form.html', {'form': form})
(Note I've assumed your form method is post rather than get - I know this isn't great http but it's a common pattern with django)
To respond to your questions -
Don't use your own method for cleaning data. Add a clean_myquery method to your form and access it with form.fields['myquery'].clean() (or if you've called is_valid() on your form, it's accessible with just form.cleaned_data['myquery']).
You want to try and avoid passing data for processing to the template. Do as much processing as you can in the view, then render the template. However if you want to pass myquery as a string for the template to render, then add it in to the context dictionary (the second non-key-word argument) in render_to_response -
return render_to_response('search.html', {'object_list': object_list, 'myquery': my query})
The post data is constructed from the form fields. You don't have a form field thequery. The view is processing the POST data - it's not creating it that's done by the html (which in turn is constructed by the Form class). Your variable thequery is declared in the view.
Django's URL dispatcher ignores query strings in the URL so http://your_site.com/ssearch will be processed by the same view as http://your_site.com/search?myquery=findstuff. Simply change the html in the template from <form method='post'> to and access the data in django with request.GET. (You'll need to change the code from the view I described above to include a new check to see whether you're dealing with a form submission or just rendering a blank form)
Have a good read of the docs on views, forms and the url dispatcher.

Categories