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

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'),
]

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'),

python routing regex

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.

NameError-- name 'logs' is not defined. Django-Python

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^logs/', include(logs.urls)),
]
I have written this code in main/urls.py file
code in logs/urls.py is below:-
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$/', views.index, name='index'),
]
and i am accessing http://localhost:8000/logs
Error:- NameError name 'logs' is not defined
You need to import the module you are actually referencing before adding it to your URLs file.
Assuming logs is in your import path:
import logs
Usually you would use a string with include so that you don’t have to import the module;
url(r'^logs/', include('logs.urls')
Also, you should remove the slash from the end of your regex for the index view. The dollar marks the end of the string, so r'^$/' will never match.
url(r'^$', views.index, name='index'),
Please check the docs. It should help!
include() function takes a list/tuple of url(), path(), re_path() and includes them to django routing system.
It also requires app's name: you can define it in include(("enter here your app's name", app.urls))
or in an app itself, in urls.py at a modular level. app_name = 'enter here your apps'name '
But only include() isn't enough, you have to define it in path() or smth that is used for routing (e.g re_path(), url() (deprecated in django 2.0))
For full details: include() function.
So, the full poiting to app's urls look so:
from django.conf.urls import url, include
from . import views
from . import app
urlpatterns = [
url(r'^', include(("app", app.urls)), name='index'),
]
I recommend you to use path() and re_path() if you use Django 2.0 >
I noticed that you use regex incorrectly little bit
url(r'^$/', views.index, name='index'),
You type $ before /, it's incorrect, $ means the end of expression and must be used in the end of expression.
Unfortunately I can't tell you all the details in the post. You should read Django docs. Go through django tutorial for start.
I'm sure it will help!

Django URL patterns not being read

So I am following a Django tutorial and I have reached the stage where I add my own URL patterns to the urls.py file.
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url((r'^webapp/', include('webapp.urls'))),
url(r'^admin/', admin.site.urls),
]
the problem is that when I run the server Django doesnt even search for the pattern. I'm not sure what I'm doing wrong here
webapp.urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
]
Make sure the directory structure is:
app
app
urls.py
webapp
urls.py
You write about a file named webapp.urls.py that is unusable by django
You pass wrong arguments for url().
Wrong:
url((r'^webapp/', include('webapp.urls')))
Right:
url(r'^webapp/', include('webapp.urls'))

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),
]

Categories