I need to change url path just for one chosen article.
This is how it looks for all of my articles:
site.com/articles/some-article
I would like to create some condition just for one chosen article.
site.com/chosen-article
is it possible?
urls.py
urlpatterns = [
url(r'^articles/', include('mysite.articles.urls')),
]
You can create that separate url path in your articles urls:
url(r'^some-article', SomeArticleView.as_view(), name='some-article'),
In the SomeArticleView, just return the wanted article.
EDIT: To better match the question, the url and view should be:
url(r'^chosen-article', ChosenArticleView.as_view(), name='chosen-article')
Finally, it can be whatever we chose.
Related
I need to configure Django urls like this:
/<slug_category>/
/<slug_category>/<slug_sub_category>/
/<slug_category>/<slug_post>/
I've tried this. The problem is that the /<slug_category>/<slug_sub_category>/ and /<slug_category>/<slug_post>/ give conflict.
urlpatterns = [
url(r'^(?P<category_slug>[\w-]+)$', views.category),
url(r'^(?P<category_slug>[\w-]+)/(?P<slug_subcategory>[\w-]+)/$', views.category),
url(r'^(?P<category_slug>[\w-]+)/(?P<post_slug>[\w-]+)/$', views.post),
]
Is it possible to do that? Can someone help me?
Thanks!
You cannot use two different URLs because they have the same pattern (as has been pointed out in the comments). The solution is to use the same URL and to fetch the content accordingly. For example
urlpatterns = [
url(r'^(?P<category_slug>[\w-]+)$', views.category),
url(r'^(?P<category_slug>[\w-]+)/(?P<slug_subcategory>[\w-]+)/$', views.cat_or_post),
]
And then you have a function that will try to fetch a post and if that fails pass it to views.category. Something like this:
def cat_or_post(request,category_slug,slug_subcategory):
try:
post = Post.objects.get(slug=slug_subcategory)
# put the rendering code here
except Post.DoesnotExist:
return category(request,category_slug,slug_subcategory)
I have two url patterns in Django:
urlpatterns += patterns('',
url(r'^(?P<song_name>.+)-(?P<dj_slug>.+)-(?P<song_id>.+)/$', songs.dj_song, name='dj_song'),
url(r'^(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$', songs.trending_song, name='trending_song'),
)
When I visit a URL of the first pattern, it opens it correctly. However if I try and visit a URL of the second pattern, it tries to access the first view again. The variables song_name, dj_slug, artist_slugare strings and song_id is an integer.
What should be the URL patterns for such a case with similar URL structure?
Both urls use the same regex. I removed the group names and get:
url(r'^(.+)-(.+)-(.+)/$', songs.dj_song, name='dj_song'),
url(r'^(.+)-(.+)-(.+)/$', songs.trending_song, name='trending_song'),
Of course django uses the first match.
You should use different urls for different views. For example add the prefix to the second url:
url(r'^trending/(?P<song_name>.+)-(?P<artist_slug>.+)-(?P<song_id>.+)/$',
songs.trending_song, name='trending_song'),
So I have two models in the same app that have pretty much identical url structures:
urlpatterns = patterns('',
#....
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^(?P<slug>[\w-]+)/$', PrizeCompanyDetailView.as_view(), name="prizecompany"),
url(r'^(?P<slug>[\w-]+)/$', 'PrizeType_Index', name="prizetype"),
url(r'^(?P<company>[\w-]+)/(?P<slug>[\w-]+)/$', 'PrizeItem_Index', name="prizepage"),
))),
# Old Redirects
)
The problems here being Reviews and PrizeType. I want my urls structured so that a user looking for prizes under a certain category goes to /prizes/prizetype. But if they want to see prizes under a certain company, then they'd go to /prizes/companyslug/. However, these two urls will naturally conflict. I can always just change the url structure, though I'd rather not. I just want to know if there are any ways around this that don't involve changing the url structure.
I would suggest writing a helper view function, which checks whether the inputted url corresponds to a company or a category, and then redirecting the request to the appropriate page.
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^(?P<slug>[\w-]+)/$', prizehelper, name="prizehelper),
where, you can check within prizehelper, if it is a company or a category and move on accordingly.
Another approach could be, to change your url structure, and reflect which type of url it is
url(r'^prizes/', include(patterns('prizes.views',
url(r'^$', 'PrizeStore_Index', name="prizestore"),
url(r'^company/(?P<slug>[\w-]+)/$', PrizeCompanyDetailView.as_view(), name="prizecompany"),
url(r'^category/(?P<slug>[\w-]+)/$', 'PrizeType_Index', name="prizetype"),
Have a single urlconf entry that goes to a view which figures out which type is being examined and then dispatches to the appropriate view for that type.
I am using Django 1.7 with Mezzanine.
URL of my pages has a prefix www.example.com/example
So I use:
FORCE_SCRIPT_NAME = '/example'
It works for default pages like blog. Blog has set url blog and it goes to /example/blog. But if I create custom link (for example in admin), it does not work. It skip /example in URL and goes directly to /.
How to fix that?
Did you wrote the pattern in urls.py?
something like this:
urlpatterns = patterns('',
url(r"^example/$",HandlingClass.as_view(),name='example'),)
Finally I found a solution.
I added FORCE_SCRIPT_NAME into TEMPLATE_ACCESSIBLE_SETTINGS in settings.py. So it is look like that now:
TEMPLATE_ACCESSIBLE_SETTINGS = ('FORCE_SCRIPT_NAME', 'ACCOUNTS_APPROVAL_REQUIRED', 'ACCOUNTS_VERIFICATION_REQUIRED', 'ADMIN_MENU_COLLAPSED', 'BITLY_ACCESS_TOKEN', 'BLOG_USE_FEATURED_IMAGE', 'COMMENTS_DISQUS_SHORTNAME', 'COMMENTS_NUM_LATEST', 'COMMENTS_DISQUS_API_PUBLIC_KEY', 'COMMENTS_DISQUS_API_SECRET_KEY', 'COMMENTS_USE_RATINGS', 'DEV_SERVER', 'FORMS_USE_HTML5', 'GRAPPELLI_INSTALLED', 'GOOGLE_ANALYTICS_ID', 'JQUERY_FILENAME', 'LOGIN_URL', 'LOGOUT_URL', 'SITE_TITLE', 'SITE_TAGLINE', 'USE_L10N')
Now is possible to extend the urls in patterns easily:
{{ settings.FORCE_SCRIPT_NAME }}/rest/of/url
Everything works now.
I'm trying to use one app to satisfy multiple url paths. That is to say, I want the url /blog/ and /job/ to use the same app, but different views. There are a number of ways to do this I'm sure, but none of them seem very clean. Here's what I'm doing right now
# /urls.py
urlpatterns = patterns("",
(r"^(blog|job)/", include("myproject.myapp.urls")),
)
# /myapp/urls.py
urlpatterns = patterns("myproject.myapp.views",
(r"^(?P<id>\d+)/edit/$", "myproject.myapp.views.edit"),
(r"^(?P<id>\d+)/delete/$", "myproject.myapp.views.delete"),
(r"^(?P<id>\d+)/update/$", "myproject.myapp.views.update"),
(r"^insert/$", "myproject.myapp.views.insert"),
)
urlpatterns += patterns("",
(r"^(?P<object_id>\d+)/$", "django.views.generic.list_detail.object_detail", info_dict, "NOIDEA-detail"),
(r"^/$", "django.views.generic.list_detail.object_list", info_dict, "NOIDEA-community"),
)
# /myapp/views.py
def edit(request, type, id):
if (type == "blog"):
editBlog(request, id)
else (type == "job")
editJob(request, id)
def editBlog(request, id):
# some code
def editJob(request, id):
# some code
I've ended up breaking all of this into multiple model and view files to make the code cleaner but the above example doesn't account for things like reverse url lookups which breaks all of my template {% url %} calls.
Originally, I had blogs, jobs, events, contests, etc all living in their own apps, but all of their functionality is so similar, that it didn't make sense to leave it that way, so I attempted to combine them... and this happened. You see those "NOIDEA-detail" and "NOIDEA-community" url names on my generic views? Yeah, I don't know what to use there :-(
You can have more than one modules defining URLs. You can have /blog/ URLs in myapp/urls.py and /job/ URLs in myapp/job_urls.py. Or you can have two modules within a urls subpackage.
Alternatively you can manually prefix your url definitions:
urlpatterns = patterns("myproject.myapp.views",
(r"^jobs/(?P<id>\d+)/edit/$", "myproject.myapp.views.edit"),
(r"^jobs/(?P<id>\d+)/delete/$", "myproject.myapp.views.delete"),
(r"^jobs/(?P<id>\d+)/update/$", "myproject.myapp.views.update"),
(r"^jobs/insert/$", "myproject.myapp.views.insert"),
)
urlpatterns += patterns("",
(r"^blog/(?P<object_id>\d+)/$", "django.views.generic.list_detail.object_detail", info_dict, "NOIDEA-detail"),
(r"^blog/$", "django.views.generic.list_detail.object_list", info_dict, "NOIDEA-community"),
)
And then mount them as:
urlpatterns = patterns("",
(r"", include("myapp.urls")),
)
Personally I would go for more RESTful URL definitions though. Such as blog/(?P<post_id>\d+)/edit/$.
Looks pretty good to me. If you want reverse lookups, just have a different reverse name for each url format, even if they end up pointing to the same view.