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
Related
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.
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!
I have this code in one file called urls.py
from django.conf.urls import url
from django.contrib import admin
from myblog import urls
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', urls),
]
Im trying to redirect to another file in a module called myblog which contains another file called urls.py with the following code:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.post_list, name='post_list'),
]
my views file which contains post_list method is as follows:
from django.shortcuts import render
def post_list(request):
return render(request,'myblog/post_list.html', {})
The result is that i keep getting the following error message:
module 'myblog.urls' from 'path\to\python\file\urls.py' is not a callable or a dot-notation path
Can anyone please explain to me where i am getting it wrong. I am new to django and python and i am using a tutorial from DjangoGirls.com. They use Django 1.8 and i have Django 1.9 installed.
Looking at the tutorial, uou can see that you have forgotten to use include.
First you have to add the import
from django.conf.urls import include, url
Then change the url pattern to:
url(r'', include('blog.urls')),
Be careful that you are using the correct module name - you have myblog, but the tutorial has blog.
You need to include the urls.py to link them
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'', include(urls, namespace='MyOtherapp')),
]
Note: I've also added a namespace here, its not required, it just helps when your using the url template tag.
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),
]
I have the following urls.py file:
from django.conf.urls import patterns, url
from base import views
urlpatterns = patterns('',
url(r'^$', 'views.index', name='index'),
url(r'^item/new', 'views.newItem', name='newItem'),
url(r'^item/submitted', 'views.itemSubmitted', name='itemSubmitted'),
)
This doesnt work, it gives me an ImportError message saying that there is no module named views. When i remove the second import line above and change the lines from views.viewname to base.views.viewname it works. Does someone know why the import is not working?
Your url route list statement is using string statements to define the location of the views. Django will attempt to lazy-load view methods when required, which can be great for strange situations where importing the view methods would cause import loops. If import loops aren't a problem (which they shouldn't be), you have two ways of doing this:
from django.conf.urls import patterns, url
from base import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^item/new', views.newItem, name='newItem'),
url(r'^item/submitted', views.itemSubmitted, name='itemSubmitted'),
)
or
from django.conf.urls import patterns, url
urlpatterns = patterns('',
url(r'^$', 'base.views.index', name='index'),
url(r'^item/new', 'base.views.newItem', name='newItem'),
url(r'^item/submitted', 'base.views.itemSubmitted', name='itemSubmitted'),
)
In the former, you are passing the view method as a property for the route. In the latter, you are passing an import path to the view methods. Note that in the latter you do not need to provide an import statement for the view.
To cut down on repitition, you can also extract the repeated prefix 'base.views':
from django.conf.urls import patterns, url
urlpatterns = patterns('base.views',
url(r'^$', 'index', name='index'),
url(r'^item/new', 'newItem', name='newItem'),
url(r'^item/submitted', 'itemSubmitted', name='itemSubmitted'),
)