Basic URL Django Issue - django

This basic url issue is giving me a headache. I have two links I wish to take a fictitious user to: localhost/prediction and localhost/prediction/. The first works, the second doesn't with an error saying that the site doesn't exist. Any clue as to why?
URl Patterns:
urlpatterns = [
# ex: /prediction/
url(r'^', views.post_list),
# ex: /prediction/<number>/
url(r'^(?P<number>[0-9]+)/$', views.show_body)
]
View:
def show_body(request, number):
return HttpResponse(number)

You should change your pattern from
url(r'^', views.post_list),
to
url(r'^$', views.post_list),
There's no need to add a leading slash, because every URL has that. See the example in the Django docs.

Related

Django urls page not found

In my urls.py I include my urls for each app, for example;
from app.insight import urls as insight_urls
urlpatterns = [
path('insight/', include(insight_urls), name='insight')
]
In insight.urls I include the view and the url like this:
urlpatterns = [
path('create/', InsightAddView.as_view(), name='create-insight')
]
The error I get says:
Page not found. No insight found matching the query
However, when I change the create-insight url from create/ to create/new/ it works.
Why doesn't the create/ url work?
Thanks to #cagrias I solved the problem. I had another url matching the create/ url. This one:
path('<slug:slug>/', InsightView.as_view(), name='read-insight-slug')
I solved it by changing that one to:
path('read/<slug:slug>/', InsightView.as_view(), name='read-insight-slug'),

Django: Changing the index site? (home page)

The index site on my Django homepage stopped working because of a problem that will take a very long time to fix. The site can't be down for that long so I am trying to change the index site so that if you go to the primary url you will atleast end up on the website.
What I have done is change the urls.py file in the primary application, where I simply replaced the line
url(r'^', include('news.urls', namespace='news')),
to
url(r'^', include('events.urls', namespace='events')),
in the urlpatterns list, where news is the faulty page and events is the page that I want to be shown. However, after pushing this to live nothing changed, and for some reason my local Django development server is not working.
Did I do anything wrong, or is there anything else I have to do as well?
Thanks.
In the events app make sure you have a URL such as
......
url(r'^$', views.EventsIndex.as_view(), name='index'),
......
Replacing news.urls with events.urls may cause problems, if there are views/templates that try to reverse news urls. It would be better to leave the include as it is, and add a new URL pattern above that for the index:
from events.views import home
urlpatterns = [
url(r'^$', home, name='home'),
url(r'^', include('news.urls', namespace='news')),
...
]

How to expand error in Django dev server?

I have trouble figuring out what trailing slash that causing an error in Django. This is the error:
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'.
Remove this slash as it is unnecessary.
My urls.py looks like this:
urlpatterns += [
#url('^$', direct_to_template, {'template': 'index.html'}, name='home'),
url('^$', mezzanine.blog.views.blog_post_list, name='home'),
url('^', include('main.urls')),
url('^', include('mezzanine.urls')),
]
And in my main.urls it doesn't exist any '/' before. How can I troubleshoot where the problem exists? Can I expand the error when I run the dev server with manage.py?
Edit:
This is my main/urls.py:
from django.conf.urls import patterns, include, url
from . import views
app_name = 'main'
urlpatterns = [
# Create Blog
url(r'^create-blog/$', views.create_blog, name='create_blog'),
# Lists
url(r'^blogs/$', views.blog_list, name='blog_list'),
url(r'^blogs/new/$', views.blogs_new, name='blogs_new'),
url(r'^top/$', views.top_lists, name='top_lists'),
url(r'^latest/$', views.latest_posts, name='latest_posts'),
# Tags
url(r'^tags/(?P<tag>[\w-]+)/$', views.tag, name='tag'),
url(r'^tags/$', views.tags, name='tags'),
# Avatars
url(r'^avatar/', include('avatar.urls')),
# Various
url(r'^welcome/$', views.welcome, name='welcome'),
url(r'^faq/$', views.faq, name='faq'),
url(r'^contact-us/$', views.contact, name='contact'),
]
Looking over the question again, perhaps you have literally defined a pattern as ^/ somewhere.
?: (urls.W002) Your URL pattern '^/' has a regex beginning with a '/'. Remove this slash as it is unnecessary.
This system check by django is printing out the pattern that is causing problems. I made a quick django example, and when I put in a url such as...
urlpatterns = [
url('^test1/$', views.test1, name='test1'),
url('^/test2/$', views.test2, name='test2'),
]
?: (urls.W002) Your URL pattern '^/test2/$' [name='test2'] has a regex beginning with a '/'. Remove this slash as it is unnecessary.
As you can see it is giving me my URL pattern exactly as it is defined, ^/test2/$. I think your problem is likely in some other part of your urls.py file or one of the other urls.py files, most likely an include because you do not have the trailing $ in the pattern nor a name for the view.
Try searching through your project for a url defined as '^/' is my recommendation.

Django index url confusion

Hi thanks for looking into this.
I have been following Django's tutorial on URLs and got a bit confused/stuck on this part:
https://docs.djangoproject.com/en/1.4/intro/tutorial03/#decoupling-the-urlconfs
what I do not understand is if, say, on page mypage.com I provide only 2 possible URLs for mypage.com/polls and mypage.com/admin, what happens if the user goes to mypage.com? Obviously, I thought, the user will need to see some sort of 'welcome' page so I decided to add another URL to that urls.py:
urlpatterns = patterns('',
url(r'^/', 'myapp.views.welcome'), #when it's just mysite.com
url(r'^myapp/', include('myapp.urls')), #includes everything with mysite.com/myapp/...
url(r'^admin/', include(admin.site.urls)),
)
But then I get redirected to that welcome view from whichever page, whether i go to /myapp or not. So, I decided to create a views.py file outside myapps folder and put that welcome page there, and it seems to have worked, apart from that I get a 404.
I am so confused! Could you explain in lamers' terms please
Thanks,
blargie-bla
It should be
url(r'^$', 'myapp.views.welcome')
otherwise any URL will match the pattern. Django will call the view for the first pattern in urlpatterns that matches, so you need to be specific and include the end-of-the-line character ($) into the pattern.

Django redirect url parsed as a query string in firefox

I'm trying to create a filtered FAQ page in Django. It filters by three categories, and defaults to 'all' for all three when someone hits the root URL. From urls.py:
keywords = ('key1','key2','key3')
searchurl = r'^category1/(?P<%s>\w{1,50})/category2/(?P<%s>\w{1,50})/category3/(?P<%s>\w{1,50})/$' % keywords
searchall = dict(zip(keywords,['all']*len(keywords)))
urlpatterns = patterns('my.path.views',
url(searchurl, 'faq', name='search_view'),
)
urlpatterns += patterns('django.views.generic.simple',
url(r'^$', 'redirect_to', {'url': searchurl, 'kwargs': searchall}, name='default_search'),
)
This has all been working fine in my testing in Safari. However, when I tried it in Firefox, navigating to the root URL returned a Page Not Found error. It had re-directed to "root/^category1/(/", as if the regular expression had been passed as a URL and everything after the first ? was interpreted as a query string. Any idea what might be causing this?
Thanks!
In your url pattern default_search, searchurl should be a url string, not a regular expression.
Looking at the Django docs on redirect_to, it looks like you can use string substitution from parameters captured from the url. You cannot substitute the searchall kwargs into the regex as you are trying. The following should work:
searchallurl = 'category1/all/category2/all/category3/all/'
url(r'^$', 'redirect_to', {'url': searchallurl,}, name='default_search'),
However if I understand your url config correctly, you don't need to redirect from the root url. Insead, call your faq view, with searchall as the optional dictionary:
url(r'^$', 'faq', searchall, name='default_search')