stuck at URL patterns of django - regex

Hi I am using following URL patterns in django and I am wondering that where is problem and why it is not able to find it. Following screenshot shows my URL as well as expected URLs.
Please tell that where I am making mistake. Please tell if some thing else I can tell to understand problem. I was looking on it for almost an hour or 2 and then I posted it on stackoverflow.com so hope I will get some solution.
Adding urls.py here:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'ecomstore.views.home', name='home'),
#url(r'^catalog/', 'preview.views.home'),
# url(r'^ecomstore/', include('ecomstore.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'',include('catalog.urls')),
)
Then urls.py of catalog app:
from django.conf.urls.defaults import *
urlpatterns=patterns('catalog.views',
(r'^$','index',{'template_name':'catalog/index.html'},'catalog_home'),
(r'^category/(?P<category_slug>[-\w]+)/$','show_category',{'template_name':'catalog/category.html'},'catalog_category'),
(r'^product/(?P<product_slug>[-_\w+])/$','show_product',{'template_name':'catalog/product.html'},'catalog_product'),
)
thanks in advance guys.

referring to my comment: at least one thing wrong is that you have an extra slash before "product". You want ^product, not ^/product. That might be all but it's a little hard to tell from the pic.
also, correct me if I'm wrong, because I haven't used regexes in a while, but doesn't [-_\w+] only match - OR _ OR \w+?
"[ ]
Match anything inside the square brackets for ONE character position once and only once, for example, [12] means match the target to 1 and if that does not match then match the target to 2 while [0123456789] means match to any character in the range 0 to 9."
http://www.zytrax.com/tech/web/regex.htm
so you'd want... [-_]\w+ ?

Related

URLpattern match doesn't work as expected

What I want
I'm running the very first steps of the Django Project tutorial and there's already something i can't get right.
The idea behind include() is to make it easy to plug-and-play URLs. Since polls are in their own URLconf (polls/urls.py), they can be placed under “/polls/”, or under “/fun_polls/”, or under “/content/polls/”, or any other path root, and the app will still work.
I built everything as needed and my 'shelves' server works fine (debug here, running on localhost).
I'm setting the urlpatterns in shelves.urls, the first of which tries to include 'bluebook.urls'.
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('bluebook/', include('bluebook.urls')),
path('admin/', admin.site.urls),
]
When I go to http://127.0.0.1:8000/bluebook/, it works fine and loads the views.index I set up.
What doesn't work
When I go to http://127.0.0.1:8000/thebluebook/, it throws a 404. It goes as far as defining regex match not working :
Using the URLconf defined in shelves.urls, Django tried these URL patterns, in this order:
1. bluebook/
2. admin/
The current path, thebluebook/, didn't match any of these.
It does the same with http://127.0.0.1:8000/the_bluebook/ or http://127.0.0.1:8000/go/bluebook/.
What I tried
I haven't written much code so there's not much to join to this summary. Although the error message only mentions shelves.urls, i checked that bluebook.urls is set to accept any regex after the redirection from shelves.urls :
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
]
I checked Django Tutorial Part 1 Error: URL does not match URL patterns and tried to change ROOT_URLCONF from 'shelves.urls' to 'urls', but it throws a ERR_CONNECTION_REFUSED instead of a 404, whatever I put after http://127.0.0.1:8000/, basically the site doesn't work anymore.
I checked out Page not found 404 on Django site? but my site's urls.py is in the child folder mysite/, not the parent mysite/.
I also tried
urlpatterns = [
path('bluebook/', 'bluebook.urls'),
path('admin/', admin.site.urls),
]
but it doesn't work either.
Basically, all my setup looks right to me as calling the exact URLpattern works as expected. Only the regex matching doesn't seem to work.
FWIW, I'm using JetBrain's PyCharm to edit the code and setup the venv, without the plugin, but I don't see it influencing the running of Django code.
Python-version : Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Django version : 2.0.7
PyCharm version : 2018.1.4
Chrome version : Version 67.0.3396.99 (Official Build) (64-bit)
You didn't include 'thebluebook' in your urls. you have path('bluebook/', include('bluebook.urls')), and path('', views.index, name='index'),
which means you can do http://127.0.0.1:8000/bluebook/any/thing/here/ but you can not http://127.0.0.1:8000/any/thing/here/bluebook/.

Syntax error when manage.py runserver

I am making my first website with Python and django. Everything went well until I got a an error for no obvious reason.
It says syntax error, but I checked with django and I can see no problems.
Any help will be much appreciated.
Possibly an indentation problem? Make sure the indentation is consistent in the file and on that line.
Also post all the urls.py code please.
Your urls.py is missing a comma
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('personal.urls')) # <-- Missing Here
url(r'^blog/', include('blog.urls')),
]
Also, be sure that your URLs in personal.urls and blog.urls end in $, as well as ^admin/. As Alasdair pointed out, don't include a $ in your regexes that have an include. That is, ^blog/ remains as is, but any URLs included from blog.urls should end with a $.

Django url mapping for similar patterns

My dir structure is as follows:
mainFolder
--myProject
---urls.py
--mysite
---urls.py
In myProject folder's urls.py I have:
url(r'^$', 'mysite.views.home', name='home'),
I changed it to
url(r'^$', include(mysite.urls)),
I would like my application to use mysite.urls.py for all requests, e.g.
localhost:8000/ OR
localhost:8000/abc OR
localhost:8000/def
etcetera
How do I configure the url parameters in myProject.urls.py in order to do this?
url(r'', include(mysite.urls)),
You have to remove the '$' from the url pattern.
You can use the following syntax
url(r'^', include(mysite.urls))
More info on why "^$" is used can be found here

404 on django app when address doesn't have extension (in mezzanine)

When I go to http://localhost:8000/getAndAnalyzePosts/index it 404s and adds a trailing slash (it does this regardless of whether I have APPEND_SLASH = True or APPEND_SLASH = False
Going to http://localhost:8000/getAndAnalyzePosts/test doesn't work but http://localhost:8000/getAndAnalyzePosts/test.anything does.
I really want to use '^$' for the index but that isn't working either.
I have this app in a mezzanine project, I haven't tried putting it into a regular django project - probably should do that next. The rest of my project works fine (using mezzanine's default apps)
getAndAnalyzePosts/urls.py
from django.conf.urls import patterns, url
from getAndAnalyzePosts import views
urlpatterns = patterns('',
url(r'^index$', views.index, name='index'),
url(r'^test.+$', views.test, name='test'),
url(r'^getSentiment$', views.getSentiment, name='getSentiment'),
)
note: getSentiment wants post variables so I am not really testing that directly
urls.py (main project)
from __future__ import unicode_literals
from django.conf.urls import patterns, include, url
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from mezzanine.core.views import direct_to_template
admin.autodiscover()
urlpatterns = i18n_patterns("",
("^admin/", include(admin.site.urls)),
)
urlpatterns += patterns('',
url(r"^$", direct_to_template, {"template": "index.html"}, name="home"),
(r"^", include("mezzanine.urls")),
url(r'^getAndAnalyzePosts/', include('getAndAnalyzePosts.urls', namespace="getAndAnalyzePosts")),
)
handler404 = "mezzanine.core.views.page_not_found"
handler500 = "mezzanine.core.views.server_error"
Also, I run python manage.py show_urls from django-extensions and it returns:
/getAndAnalyzePosts/getSentiment getAndAnalyzePosts.views.getSentiment getSentiment
/getAndAnalyzePosts/index getAndAnalyzePosts.views.index index
/getAndAnalyzePosts/test.+ getAndAnalyzePosts.views.test test
The project urls.py you've pasted here at some stage had some comments in great big capital letters describing the exact problem you're facing, here's their original source:
https://github.com/stephenmcd/mezzanine/blob/master/mezzanine/project_template/urls.py#L63-L66
Append slash option makes sure that if the incoming url does not have trailing slash and no url gets match, then it retires after adding trailing slash.
What it does not do is retry after removing trailing slash from incoming url.
Since incoming url does have a trailing slash, you need a / before $
Add / at the end, before $ in all your urls, (r'^getSentiment/$)
Let append slash be true and it should work.
.+ works because . in regex matching anything except newline and you added + after it so it matches any character any number of times, to match . exactly you need to escape it (.)
Making it ^text\..+$

Django include url causes rendering error?

I have a working django project. I wrote a small app - pm - and I tried to include its urls.py in the active project:
urlpatterns = patterns('',
# ... some urls here
url(r'^$', views.home, name='vw_home'),
# I added the following line:
(r'^pm/', include('pm.urls')),
Once I access the main web page, I receive the following error:
TemplateSyntaxError at /
Caught error while rendering: syntax error
and the debug shows the problem in the following line:
Home
If I remove the last url pattern (the include()), the page renders without any problem.
How can this be fixed?
EDIT:
Adding the urls.py of the pm app:
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns("pm.views",
url(r'^inbox/$', 'inbox', {'folder': 'inbox'}, name='vw_inbox'),
url(r'^sent/$', 'inbox', {'folder': 'sent'}, name='vw_sent'),
url(r'^message/(?<message_id>\w+)/$', 'read_message', name='vw_read_message'),
url(r'^compose/(?P<profile_id>\w+)/$', 'compose_message', name='vw_compose_message'),
url(r'^reply/(?P<message_id>\w+)/$', 'compose_message', name='vw_reply_message'),
)
url(r'^message/(?<message_id>\w+)/$', 'read_message', name='vw_read_message'),
You missed ?P