Django error in urlpatterns: No module named views - python

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

Related

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.

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.

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!

'views' is not defined in Django 1.10

My app is called courses and I can't import my urls.
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^courses/', course.urls),
url(r'^admin/', admin.site.urls),
url(r'^$', views.hello_world),
]
And I always get this error
NameError: name 'courses' is not defined
You need to use the include function when trying to include other urls
url(r'^courses/', include(course.urls)),
Note: admin.site.urls is a special case and doesn't require the use of include since it does magic

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

Categories