python routing regex - python

I am struggling with urls.py.
Error:
[pylint] E0602:Undefined variable 'patterns'
In code:
from django.conf.urls import *
from django.contrib import admin
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^$', 'notes.views.home', name='home'),
url(r'^admin/', include(admin.site.urls)),
)
I am following the tutorial on link : Django Tutorial: Building a note taking app
Problem number 2.
Same problem in tutorial : Simple Django Web Application Tutorial
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'\^admin/', admin.site.urls),
url(r'\^myrestaurants/', include('myrestaurants.urls', namespace='myrestaurants')),
]

You are following a tutorial for Django 1.7, but using yourself Django 2.0.7. Django 2 isn't backward compatible with older versions and this can lead to pitfalls.
I'd strongly recommend you to follow a tutorial for the current version. The official documentation has a good tutorial.
Unless you are tasked to maintain a legacy Django project, you don't need to know how things have been done in past versions. Start with the current version, which is now Django 2, and learn how the things have to be done. This is my opinion and might eventually colide with the beliefs of other developers.
The problem you're facing can be resolved with some adaptions. First of all, the line:
from django.conf.urls import *
is a bad practice, regardless of the Django or Python version you're using.
Import the modules you need explicitly. Django 2 has a slightly different approach when it comes to routes. Although you could still use url, the new way is to use path. The urlpatterns should be a list containing path objects.
The proper import should be:
from django.urls import path, include
Your urlpatterns should look like:
urlpatterns = [
path('', 'notes.views.home', name='home'),
path('admin/', include(admin.site.urls)),
]
Even better approach would be to define urls.py in your apps and include it in the main urls.py in your project directory:
urlpatterns = [
path('myrestaurants/', include('myrestaurants.urls', namespace='mysrestaurants'),
path('admin/', include(admin.site.urls)),
]

This is deprecated from 1.10:
urlpatterns = patterns('')
So make sure you're using a compatible version of Django.

Related

Every url in a django app only renders home.html

So, I am not really lucky with latest django version tutorials, so I've had some problems with things that changed between some versions. One of this things is: althought I do exactly as I read/watch in the tutorials I always get the same result - all urls redirect to the same HTML page.
Here is my root urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('theblog.urls')),
]
Here is my app urls:
from django.conf.urls import url, include
from .views import HomeView, ArticleDetailView
urlpatterns = [
url('', HomeView.as_view(), name='home'),
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
]
For example, when I go to localhost:8000/articles/1 (or any other pk), it renders home.html (HomeView class) as if it was localhost:8000/.
Hope you can help me. Thanks!
There are two things wrong with your code.
url('^article/<int:pk>', ArticleDetailView.as_view(), name='article-detail'),
This will not work. If you want to use url. you can't use <int:pk>, you need to use a RegEx:
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail")
Note that this will be deprecated in the future and if you are using django >=2.0 you should use path:
path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail")
However this will still direct you to the wrong view. django stops after the first URL pattern match.
Switch them around to fix that:
urlpatterns = [
url("article/([0-9]+)/", ArticleDetailView.as_view(), name="article-detail"),
# path("article/<int:article>/", ArticleDetailView.as_view(), name="article-detail"), # alternative with path instead of url
url('', HomeView.as_view(), name='home')
]
It may be due to this line
url('', HomeView.as_view(), name='home'),
Because url wraps re_path there may be some logic which will treat the blank regex string as a wildcard. Try changing it to '/'
url('/', HomeView.as_view(), name='home'),

Issues with path() in urls.py in django 2.0.5

I am trying to do the following thing in urls.py but Django 2.0.5 doesn't seem to support url(). Instead of it, I used path() but still, its throwing invalid syntax error.
Can someone give a clearer picture of path() as it seems to be not supporting regex.
Providing the code here:
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('$', home_page)
path('admin/', admin.site.urls),
]
You miss a ,, and $ is unnecessary
from django.contrib import admin
from django.urls import path
from .views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
Django2 has 2 functions for URLconfs, path(), re_path().
You can use regex paths (regular expression based paths) with re_path(), so remove $ and place , between two consecutive paths.
Note: Let suppose your app name is my_django_app created by python manage.py startapp my_django_app command.
I created a new Django app named my_django_app and tried, it works fine. I have the following code in my urls.py file.
"""my_django_proj URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from my_django_app.views import home_page
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls),
]
References: https://docs.djangoproject.com/en/2.0/topics/http/urls/
django 2.0 - including urls file from an app to the root urls file
Thanks.
If you prefer to use url instead of path, that will work just fine. You just have to import from django.conf.urls instead. So your import statement should look like this:
from django.conf.urls import url
Django says on their documentation page that this feature will likely be deprecated in future versions to re-path, however, url still works fine for me, and I'm running Django 2.0.7... so, I imagine it would work with yours as well. I guess because of this, with Django version 2 and above, it nows decides when it creates the boilerplate project that instead of importing urls from django.conf.urls, it imports path from django.urls. (Note: PATH doesn't allow for regex)
What I typically do, is create an app specific urls.py. In that urls.py I'll import url from django.conf.urls and have my specific app level urls there:
from django.conf.urls import url
from app_name import views # have to import views
urlpatterns = [
url(r'^$', views.index),
url(r'^users$',views.users),
]
Then in the project level urls.py I'll add the include module as so I can link it to my app specific urls.py file:
from django.conf.urls import url, include
from django.contrib import admin
from app_name import views
urlpatterns = [
url(r'^',include('app_name.urls')),
url(r'^admin/', admin.site.urls),
]
(Note: If you have a separate folder in between such that the folder structure looks something like mainproject>apps>app_name>(settings.py, views.py, admin.py etc...) you will have to create an __init__.py file in the apps folder as so Django can recognize the module.
urlpatterns = [
path('', home_page),
path('admin/', admin.site.urls)
]
As stated in the above answers, the $ is unnecessary while using path(). You are getting a syntax error due to the comma after admin.site.urls) which should be removed.

Django1.11 - URLconf does not appear to have any patterns

I'm a new user of django and i have some difficulties.
I follow a Django 1.8 tutorial and i have Django1.11. the problem is about urls.py files & patterns
Here my code, blog/urls.py:
from django.conf.urls import url
from . import views
urlpattern = [
url(r'^accueil/$', views.home, name='home'),
]
first/urls.py:
from django.conf.urls import url include
urlpattern = [
url(r'^blog/', include('blog/urls')),
]
And when i use runserver command, it display :
URLconf '<module 'blog.urls'>'does not appear to have any patterns in it
I read somewhere that patterns are removed since 1.10 and it's an old syntax now but i don't found the solution.
Any idea ?
Django 1.11 is not released. You should use the actual released version, 1.10. Also, you should use the actual tutorial for your version.
However, the problem is that both of your files should define urlpatterns with an s, not urlpattern.
I think your need this page.URL dispatcher
Your code is fault, I think your need to change it:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^accueil/$', views.home, name='home'),
]

Django error in urlpatterns: No module named views

In Django 1.7.x this construct was working:
# urls.py
import views
urlpatterns = ('',
url(r'^$', views.index)
)
In Django 1.8.X it stopped working. Now I get this error message when I run the default Django server:
No module named 'views'
I also tried this:
from system.views import *
urlpatterns = ('',
url(r'^$', views.index)
)
This results in:
name 'views' is not defined
And this:
from system import views
urlpatterns = ('',
url(r'^$', views.index)
)
I also tried many more combinations that I've seen at stackoverflow, but none of them works. Hope someone can share what magic combination should do the trick.
EDIT
\home
\jacobian
\apps
\apps
__init__.py
settings.py
urls.py
views.py
...
\system
__init__.py
urls.py
views.py
...
I just tried to recreate this issue. It seems that you are correct and just import views no longer works. However, the following import statement works fine for me:
from . import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^$', views.index)
]
You can see an example here on the django documentation. I also think this related Stack Overflow question can clarify they reason for dot import syntax:
Q: Python "from [dot]package import ..." syntax
You should include the views.py file from the application that you have created. So try
from <your app name>.views import *
Your statement is a bit of a mix.
Before version 1.8 it was
from myapp import views
urlpatterns = patterns('',
url('^$', views.myview),
url('^other/$', views.otherview),
)
Now, from version 1.8, there is no need for the first void argument to patterns when assigning urlpatterns. Actually there is no need to call patterns at all.
Here is one example form my latest project with Django 1.8:
urlpatterns = [
url(r'^$', HomePage.as_view(), name='home'),
url(r'^play/', include('play.urls', namespace='play', app_name='play')),
]
And as described in the Django 1.8 release docs:
Thus patterns() serves little purpose and is a burden when teaching
new users (answering the newbie’s question “why do I need this empty
string as the first argument to patterns()?”). For these reasons, we
are deprecating it. Updating your code is as simple as ensuring that
urlpatterns is a list of django.conf.urls.url() instances.
For example:
from django.conf.urls import url
from myapp import views
urlpatterns = [
url('^$', views.myview),
url('^other/$', views.otherview),
]

Django's admin documentation generator not working

I installed docutils.
Then included 'django.contrib.admindocs' in my 'INSTALLED_APPS',
Then, added 'admin/doc' to my url.
When I go to admin/doc, I see the list of things like models, views, filters and etc. It is fine. But when I click on one of those, it says page not found.
Where did I go wrong?
i know this question is for long time ago but :
Add path('admin/doc/', include('django.contrib.admindocs.urls')) to your urlpatterns.
1
Make sure it’s included before the 'admin/' entry, so that requests to /admin/doc/ don’t get handled by the latter entry.
if you code like this:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('admin/doc/', include('django.contrib.admindocs.urls')),
]
change it to:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/doc/', include('django.contrib.admindocs.urls')),
path('admin/', admin.site.urls),
]
The Django admin documentation generator

Categories