reverse with view function not working with multiple urls.py files - python

I'm having issues with using the reverse() function with a view-function as argument. It work's fine when I specify the URL routing in the default urls.py file. But when I have a import to a secondary urls.py file I get NoReverseMatch-errors.
So... This is how my urls.py files looks like.
demostore/demostore/urls.py
#demostore/demostore/urls.py
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('lindshop.urls', namespace="shop")),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
demostore/lindshop/urls.py:
#demostore/lindshop/urls.py
urlpatterns = [
url(r'^$', views.landing, name='index'),
...
url(r'^product-index/$', 'lindshop.core.product.views.product_index', name="product_index"),
]
My product_index view is just an empty view without any arguments that looks like this:
def product_index(request):
return TemplateResponse(request, "index.html")
Now... When I call
reverse('lindshop.core.product.views.product_index')
I get a NoReverseMatch error. HOWEVER, if I put my url(r'^product-index/$'...) in the demostore/demostore.urls.py, the reverse() works fine. But this is not what I'm looking for, I want to keep all URL-routing in my custom app's urls.py.

You have added a namespace, so you need to resolve the urls correctly (refer to the documentation for more details):
reverse('shop:product_index')
Obviously I don't want to use Namespace in this case. If you read my
question I want to call it by function name, which IS working when I
put the routing in the demostore/demostore/urls.py file. But not in
the included lindshop/urls.py file. And yes... it does work with
reverse('shop:product_index'). But that's not what I want.
If you read the documentation for reverse, you'll find this snippet:
Deprecated since version 1.8:
The ability to reverse using the Python path, e.g.
reverse('news.views.archive'), has been deprecated.

Related

How to configure urls.py in django to redirect to another file's urls?

I'm following a django tutorial that is a little outdated and in the urls.py file of the first app's directory we need to configure where to direct django to for any url starting with 'notes/'.
There are two separate 'apps' inside the project. I'm in the first one, not notes.
This is the code currently. I added include to import statement:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(url(r’^notes/‘, include('notes.urls'))),
]
Inside the urlpatterns object, on the first line, path('admin/', admin.site.urls), comes predefined, but I need to add a redirect such that django goes to a different app, called 'notes' and searches there for its entry point, since all of the urls will begin with ‘notes/’.
The tutorial says to use a regular expression and uses this code:
url(r’^notes/‘, include(notes.urls))
so that any url that starts with 'notes/' should be redirected to this other file notes.urls.
However the predefined ones that currently come out of the box with a django project start with path.
I enclosed my notes/n redirect line in path, but not sure if this is correct. Should I instead directly write:
url(r’^notes/‘, include(notes.urls))
Also, do I need to delete the first line provided?
path('admin/', admin.site.urls),
The tutorial has:
urlpatterns = patterns('',
url(r’^notes/‘, include(notes.urls)),
)
and no admin urls line. It's from 2014 I believe.
Simply do:
path('notes/', include('notes.urls'))

Django redirecting to wrong view

I have an app called 'vocab', which so far contains a CreateView and a ListView. In my project urls.py file, I have the following:
urlpatterns = [
path('admin/', admin.site.urls),
path('',views.HomePage.as_view(),name='home'),
re_path(r"^accounts/", include("accounts.urls", namespace="accounts")),
re_path(r"^accounts/", include("django.contrib.auth.urls")),
re_path(r"^logout/$", views.LogOutPage.as_view(), name="logout"),
re_path(r"^vocab/", include("vocab.urls", namespace="vocab")),
]
And then, in my app urls.py file, I have the following:
urlpatterns = [
path('list/', views.WordList.as_view(), name="list"),
path("home/",views.VocabHome.as_view(),name='home'),
path("create/", views.CreateWord.as_view(), name="create"),
]
And in my html templates I have links to these views. The home and list views work, but when I click on the 'create' link, it takes me to 'list' instead. In the command line I can see that it is accessing /vocab/list/? instead of /vocab/create/. I know the create view works, because if I type it in manually it goes there. And if I delete all the other views it works as well. So it is as if django cannot find the 'create' view when there are others. I've tried my best to solve this problem by making sure all the paths are unique, and I've tried using regular expressions, but it still doesn't work. Any help would greatly be appreciated!
I found the mistake...
I had a missing form closing tag in my html. This resulted in my second form (and therefore link) being invalid.

Django - How include() can be used to create plug and play URLs?

As mentioned here,
the include() function allows referencing other URLconfs. Note that the regular expressions for the include() function don't have a $ (end-of-string match character) but rather a trailing slash. 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.
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.
Need clarification on second point(above) about, include() is to make it easy to plug-and-play URLs,
For one of the app(webapp) in Django project:
webapp/
__init__.py
admin.py
apps.py
migrations/
__init__.py
models.py
tests.py
urls.py
views.py
where, root URLconf is pointing to webapp.urls as shown below,
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls')),
]
here is the complete code
With webapp app, How include() can be used in creating plug and play URLS?
Maybe in a django project, you will register many apps into it, and each will have its own urlconf, at that moment, using the include() will make things simple.
You could just add a prefix for each app and using include() to combine all into root urlConf.
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.
The include() works by import_module(), you could refer to this function at this
In Django URLs, include() lets you refer to other app's URLconfs.
It basically means something like this
urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^webapp/', include('webapp.urls')),
]
webapp.urls.py
urlpatterns = [
url(r'^$', views.content),
]
Now, whenever you will call /webapp/ it will render the content view.
Plug-and-Play basically means URL /webapp/ is ready to be called. It will return anything that will come under this URL if you have a view specified for this URL.

Django urls.py: url doesn't work properly

I'm developing a webpage with Django.
I want to add/define an url for the application.
At this moment the url (dsvd/) doen't work properly. It shows only table data but no css and background.
here is the code for the main urls.py file.
from django.conf.urls import patterns, include, url
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'kleedkamer_overview.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^admin/', include(admin.site.urls)),
url(r'^', include('kleedkamer_overview.urls')),
url(r'^dsvd/', 'kleedkamer_overview.views.indeling'),
)
and here the code inside the application urls.py file
from django.conf.urls import patterns, url
from kleedkamer_overview import views
urlpatterns = patterns ('',
url(r'^$',views.indeling, name='indeling'),
)
Is there anyone who can help me find the problem?
Greetz.
It looks like line is giving you trouble:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
It seems like you are trying to map this URL to kleedkamer_overview.views.indeling but the URL regrex does not end with a '$'. Urls.py files that are mapped to other apps look like this:
url(r'^admin/', include(admin.site.urls))
Notice the include function call and that the regrex expression r'^admin/' does not end with a $
However mapping to a specific view is a bit different and it looks like this:
url(r'^$', 'kleedkamer_overview.views.home', name='home')
Notice that this time, where the include() function call would go you are instead telling Django which specific view you want to use and that the site regrex is r'^$' ending with a $.
Try changing this:
url(r'^dsvd/', 'kleedkamer_overview.views.indeling')
to this:
url(r'^dsvd/$', 'kleedkamer_overview.views.indeling')
Edit:
I saw your comments and while the urls.py is not the source of your problem, what I said is still valid because the urls.py was malformed. You should not even have the offending line there at all because you have already included the urls.py from kleedkamer_overview , there is no need to include it twice. It isn't DRY and it is just bad practice in general. This is why this still works despite being malformed because sites are looked for in order, in this case first it looks for /admin then / and does not reach /dsvd because it was already caught by / and your malformed url mapping is NEVER reached.

Django, how to pass just the last segment of url to view

I'm attempting to pass a string from the end of a url to a view function, but I'm getting the entire url passed to render_form() i.e. if I input "myurl.me/prepend/identifier" I get "prepend/identifier/" when I just want "identifier"
These are my urls.py files, the top one is located in an app directory, the bottom one is in the project dir:
urlpatterns = patterns('',
url(r'^(?P<identifier>.+)$', views.render_form),
)
--
urlpatterns = patterns('',
url(r'^prepend/', include('the_ones_above.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I think it might have somehting to do with the split over 2 urlpatterns in 2 separate files, any ideas what im doing wrong?
Thanks!
edit: the view looks like: def render_form(request, identifier=None):
oh, and this is the latest django (from git)
2nd edit: removed the rather superfluous 1st url, behavior is still the same
I will suggest you to simply write you url like.
url(r'^(?P<identifier>[\w]+)/$', views.render_form),
[\w] will tell you that you can pass word character (A-Za-z0-9_).

Categories