Django URL-Patterns not working as expected when using INCLUDE - django

I am currently working my way through the Django Tutorial (Step 3) and am stuck at the part with "Decoupling the URLconfs".
What I try to do is to set up one URL-Pattern that catches lnadmin/, to redirect to the django admin, and eventually another catch-all that redirects to other patterns included from another file.
Here's my mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^lnadmin/', include(admin.site.urls)), #match admin
url(r'^test/', include('lnapp.urls')), #match test, should be a catch-all later
)
and here's the lnapp/urls.py, which is supposed to match hash/(anything)/:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('lnapp.views',
url(r'^hash/(?P<hash>.+)/$', 'hash'), #match part to load from hash
)
I had this pattern in the main url.py before, and it worked as intended.
What's happening now is that when I open (mydomain)/lnadmin/, it tries to access lnapp.views.hash (Could not import lnapp.views.hash, as no view is defined yet).
This doesn't make any sense to me, as lnadmin/ should be matched by the first pattern, and /lnadmin/ doesn't match test/hash/(anything)/. As soon as I comment out the one url in lnapp/urls.py, it redirects to the admin, as intended.
Swapping both urls in the main url.py has no effect.

The answer to my own question is: you have to define a view even for the unmatched urls, or else it will fail.

Related

How url dispatcher include apps urls

I have project urls.py
from django.contrib import admin
from django.urls import path,include
from django.contrib.auth import login
urlpatterns = [
path('admin/', admin.site.urls),
path('login/',include
('authorization.urls')),
]
My included authorization/urls.py file is followed
from django.urls import path
from . import views
urlpatterns = [
path('login',views.login_name),
]
While i started to learn url dispatcher logic i much times faced with
The current path, login/login/, didn't
match any of these.
As pointed in documentation
Whenever Django encounters include(), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
When i am trying to access my login page i see
Using the URLconf defined
in forecast.urls, Django tried these
URL patterns, in this order:
admin/
login/ login
The current path, login/, didn't match any of these.
As i see from traceback and from documentation notes while inside path function included include() function. Path parse given arguments and by examplefrom my project first of all path parse given url pattern login than when path face with include function its give parsed url pattern to included urls. Than when included urls.py parsed its face with another url pattern login its chain them login/login and give it to attached views function.
Here is my first question how i should specify url pattern while i need use include function
Am i understood correctly in included app.urls.py i shouldn't specify login pattern based on my example
Can anyone understand me how path include function work that i can debug it
you have repeated 'login', so change it as follows, so it will be available on /login
path('',include('authorization.urls')),

Page not found, URL not resolved

I am trying to call a function in my views.py by pasting a url http://127.0.0.1:8000/upload_results/UniEX_HG1_A15 in the web browser,
but the request fails and I can not see why my URL pattern does not work.
The error:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/upload_results/UniEX_HG1_A15
Using the URLconf defined in varview.urls, Django tried these URL patterns, in this order:
^$ [name='show_index']
^admin/
^upload/ [name='varview_submission']
^upload_results/(?P<project_id>[0-9A-Za-z_]+)/ [name='varview_upload_results']
^validate/(?P<project_id>[0-9A-Za-z_]+)/ [name='varview_validate']
^filterallprojects/[0-9A-Za-z_]+ [name='varview_filterallprojects']
^project/(?P<project_id>[0-9A-Za-z_]+)/wgsmetrics/ [name='varview_wgsmetrics']
^project/(?P<project_id>[0-9A-Za-z_]+)/targetgenecoverage/ [name='varview_targetgenecoverage']
^project/(?P<project_id>[0-9A-Za-z_]+)/(?P<display_option>[0-9A-Za-z_]+)/ [name='varview_project']
^media\/(?P<path>.*)$
The current path, upload_results/UniEX_HG1_A15, didn't match any of these.
And here is my urls.py:
from django.conf import settings
from django.conf.urls import url
from django.conf.urls.static import static
from django.contrib import admin
from varview import views
from varview.forms import DataUploaderForm1, DataUploaderForm2, GetProjectIdForm
urlpatterns = [
url(r'^$', views.show_index, name='show_index'),
url(r'^admin/', admin.site.urls),
url(r'^upload/', views.init_submission, name='varview_submission'),
url(r'^upload_results/(?P<project_id>[0-9A-Za-z_]+)/', views.upload_results, name='varview_upload_results'),
]
This already worked some time ago, but in the meantime I did many changes.
Latest change was to include celery (djcelery).
The index page and others still work. I already read many posts related to django-url, but could not figure it out.
Thank you for your help.
Note your URL has a trailing slash,
^upload_results/(?P<project_id>[0-9A-Za-z_]+)/
but you are trying to access a URL without the trailing slash
/upload_results/UniEX_HG1_A15
Normally, Django will redirect to the URL with the trailing slash. Perhaps your MIDDLEWARE setting is incorrect, or you have set APPEND_SLASH to False.

Django Tutorial Part 1 Error: URL does not match URL patterns

I am going throught the Django tutorial here:
https://docs.djangoproject.com/en/1.10/intro/tutorial01/
I follow the instructions exactly. But when I try to go to http://localhost:8000/polls/, I don't see the message “Hello, world. You’re at the polls index.” as expected. Instead, I get the following error:
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, polls/, didn't match any of these.
Here is my mysite/urls.py file. I am not sure why the first regex pattern in urlpatterns is not recognized.
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
You might have forgot .html extension in views.py while requesting the page.

Django Page not found

I want to create urls to go to links like this: examples.com/baiviet/post-example/
*post-example is a slug
this is my root urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'^$', include('blog.urls')),
)
Then, this is my blog/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blog import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost'),
)
My views.py:
def view_post(request, slug):
getpost = get_object_or_404(Blog, slug=slug)
return render(request, 'view_post.html', {'post':getpost})
And my view_post.html:
{{ post.content }}
The only thing I have is "Page Not Found" Error. I have tried to solve it and it takes me 2 hours before posting this question. I hope that someone can help me solve this problem. Thanks you
The reason for 404 is that in your root urlconf, you have
url(r'^$', include('blog.urls'))
Here, $ indicates end of url pattern. Change that to
url(r'^/', include('blog.urls'))
# ^ note the $ shoudl be replaced by / when you are doing an include.
Here is the relevant documentation:
Note that the regular expressions in this example don’t have a $ (end-of-string match character) but do include a trailing slash. Whenever Django encounters include() (django.conf.urls.include()), it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing.
The issue with the missing pattern, as alecxe mentions on <slug> would arise after it resolves this issue (404).
EDIT:
For you to access the homepage, you need to have a trailing / or have the setting APPEND_SLASH setting to True. Since your URL Pattern is expecting a prefix of / - Now, if you dont want it, in your root urlconf, change r'^/' to just r'^'
You have an empty capturing group configured for the url:
url(r'^baiviet/(?P<slug>)/$', views.view_post, name='viewpost')
HERE ^
You need to provide a pattern for a slug to match, e.g. alphanumeric, underscore and a dash:
url(r'^baiviet/(?P<slug>[a-zA-Z0-9_-]+)/$', views.view_post, name='viewpost')

Django basic url slugifying issues

I want to slugify my urls in my Django application. I have read many documents but I am still not sure how to do it better. I have two questions:
How to call the same view for two different urls?
I would like to call home view for both www.mysite.com and www.mysite.com/index.html
(r'^$', 'myapp.main.views.home')
(r'([-\w]+)$', 'myapp.main.views.home')
The code above sounds good but of course it raises an error as home view expects 1 parameter but 2 is given. How can I resolve this?
I have so many apps and they all have their own urls.py file. I was handle them as including their urls file to the root urls.py as
(r'^warehouse/', include('myapp.warehouse.urls')),
In that way, urls seems like www.mysite.com/warehouse/blabla/
However, I want to slugify them as www.mysite.com/warehouse_blabla.html
Slugifying is not hard but how can I resolve such url and redirect it to the blabla view in warehouse app?
Thanks
Regarding the first problem, you would be better off using a redirect for the index.html URL (better for SEO etc.)
from django.views.generic.simple import redirect_to
urlpatterns = patterns('',
url(r'^$', 'myapp.main.views.home')
url(r'^index.html$', redirect_to, {'url': '/'}),
)
Regarding the second issue, your urls.py file is just a set of regular expressions, so you have control over the URL scheme you want to use:
urlpatterns = patterns('',
url(r'^warehouse_(?P<slug>[_w]+).html$', 'warehouse.views.warehouse_detail'),
)
That said, I think you would be better sticking to the usual convention of slashes