Regular Expressions not resolved after using re_path in django urls - python

I have a problem with Django urls:
main urls.py
from django.contrib import admin
from django.urls import include, re_path, path
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^', include('tutorials.urls')),
]
tutorials/urls.py
from django.urls import re_path
from tutorials import views
urlpatterns = [
re_path(r'^api/tutorials$', views.tutorial_list),
re_path(r'^api/tutorials/(?P<pk>[0-9]+)$', views.tutorial_detail),
re_path(r'^api/tutorials/published$', views.tutorial_list_published)
]
when i go to check API via browser after python manage.py runserver i get:
Looks like regular expressions are not resolved? Any ideas?

You asked the "http://127.0.0.1:8080/api" URL.
Your urlpatterns does not contains any path which could match with this url.
I don't know where you want to go with this url, but if you add
path('api', views.tutorial_list),
In your urlpatterns list, you will go to tutorial_list view for example.
You don't need to use re_path for path without regular expression. just use path function.

Related

How can i do my url flexible in Django 1.11?

In my project with django I should do a site with flexible url. For instance:
mysite.com/register/kj-1k-32-mk
When someone write some other things after register/ I want to redirect them to my site. How can I redirect them. What should my urls.py looks like?
urls.py
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/(?P<pk>\d+)',views.guaform,name='custform'),
]
When I do like above, the part after register/ can't begin with a letter.
That's because of the \d+ pattern which only allows a sequence of digits. If you want a slug-like sequence, you can use:
from django.conf.urls import url
from django.contrib import admin
from olvapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^register/(?P<pk>[-\w]+)',views.guaform,name='custform'),
]
Note: django-1.11 is no longer supportedĀ [Django-doc] since April 2020, you should consider upgrading the Django version.

ImportError: cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0

After upgrading to Django 4.0, I get the following error when running python manage.py runserver
...
File "/path/to/myproject/myproject/urls.py", line 16, in <module>
from django.conf.urls import url
ImportError: cannot import name 'url' from 'django.conf.urls' (/path/to/my/venv/lib/python3.9/site-packages/django/conf/urls/__init__.py)
My urls.py is as follows:
from django.conf.urls
from myapp.views import home
urlpatterns = [
url(r'^$', home, name="home"),
url(r'^myapp/', include('myapp.urls'),
]
django.conf.urls.url() was deprecated in Django 3.0, and is removed in Django 4.0+.
The easiest fix is to replace url() with re_path(). re_path uses regexes like url, so you only have to update the import and replace url with re_path.
from django.urls import include, re_path
from myapp.views import home
urlpatterns = [
re_path(r'^$', home, name='home'),
re_path(r'^myapp/', include('myapp.urls'),
]
Alternatively, you could switch to using path. path() does not use regexes, so you'll have to update your URL patterns if you switch to path.
from django.urls import include, path
from myapp.views import home
urlpatterns = [
path('', home, name='home'),
path('myapp/', include('myapp.urls'),
]
If you have a large project with many URL patterns to update, you may find the django-upgrade library useful to update your urls.py files.
I think a quick fix to this problem is to do followings;
You can easily replace
from django.conf.urls import url
to this:
from django.urls import re_path as url
And keep the rest of code to be same as before.
(Thanks #Alasdair)
See in django version 4.0 it will not work.
So while installing Django in your Virtual Environment select this version
pip install django==3.2.10
This will definitely solve your error and in main urls.py do this:
from django.conf.urls import url
from django.urls import path,include

django: Cant open two different pages when I have two views for an application

I am doing django course from udemy, i did one experiment. Below is my folder structure
Project
appTwo
urls.py
ProTwo
urls.py
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^$',views.help,name='help'),
url(r'^$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^help/',include('appTwo.urls')),
url(r'^users/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Now when i try to open the page users by http://127.0.0.1:8000/users it opens the page help.html. For http://127.0.0.1:8000/help it opens help page. When I comment the first entry in urlpatterns in urls.py it opens the users page even if i try to open help page. Can anyone please guide me what wrong I am doing or its working as expected.
you need to use different patterns for each View:
urlpatterns = [
url(r'^help$', views.help, name='help'),
url(r'^users$', views.users, name='users'),
]
the name attribute is only useful for the concept of reversing, maybe further in your course?
also, the r'' strings in python are regular expressions, you might want to learn more about them.
I got the answer for this. Below two files need to be changed
appTwo/urls.py
from django.conf.urls import url
from appTwo import views
urlpatterns = [
url(r'^help$',views.help,name='help'),
url(r'^users$',views.users,name='users'),
]
ProTwo/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url,include
from appTwo import views
urlpatterns = [
url(r'^$',views.index,name='index'),
url(r'^appTwo/',include('appTwo.urls')),
path('admin/', admin.site.urls),
]
Explanation:
When I type in browser "basic url" i mean the address here it is http://127.0.0.1:8000/, it will go to ProTwo/urls.py which is the project folder. This will open the index page as per line url(r'^$',views.index,name='index'). If you need two configure two different page, give url(r'^appTwo/',include('appTwo.urls')), in the ProTwo/urls.py. This will call appTwo/urls.py. Now for help type http://127.0.0.1:8000/appTwo/help and for users type http://127.0.0.1:8000/appTwo/users.

Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order

I have a problem with Django. Actually I want to create a login panel in my new site but when I try to write address the debugger respond me a error:
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/account/login
Using the URLconf defined in firmowa.urls, Django tried these URL patterns, in this order:
admin/
^account/
The current path, account/login, didn't match any of these.
My firmowa.urls is
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(r'^account/', include('account.urls')),
]
And the project urls.py is:
from django.urls import path
from . import views
urlpatterns = [
path(r'^login/$', views.user_login, name='login'),
]
I'm looking a solution from few hours but still nothing.
path is a new feature in Django 2.0 and works different than the old url. Don't use regex in path. If you need regex, use re_path instead, but in your case there's no need for that.
Change your code to:
urlpatterns = [
path('login/', views.user_login, name='login'),
]
etc. and it should work.
More on the topic here and here.
from django.urls import path
from . import views
from django.conf.urls import url
urlpatterns = [
url( r'^$', views.index , name='index') ,
]

can not import patterns in django 1.11.4

My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.

Categories

Resources