Django import doesnt work when trying to import views - python

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

Related

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

<module 'myblog.urls' from 'path\\to\\python\\file\\urls.py'>' is not a callable or a dot-notation path

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.

how to display a view in Django?

I'm totally new to Django, and I'm trying to understand how does it work (I'm more used to PHP and Spring frameworks.
I have a project called testrun and inside it an app called graphs, so my views.py looks like:
#!/usr/bin/python
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, World. You're at the graphs index.")
then, in graphs/urls.py:
from django.conf.urls import patterns, url, include
from graphs import views
urlpatterns = patterns(
url(r'^$', views.index, name='index'),
)
finally, at testrun/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'testrun.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^graphs/', include('graphs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
However, when I try to access http://127.0.0.1:8000/graphs/ I get:
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/graphs/
Using the URLconf defined in testrun.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, graphs/, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
What am I doing wrong that I can't get that simple message to be displayed in the browser?
To expand on my comment, the first argument to patterns() function is
a prefix to apply to each view function
You can find more information here:
https://docs.djangoproject.com/en/dev/topics/http/urls/#syntax-of-the-urlpatterns-variable
Therefore in graphs/urls.py you need to fix the patterns call like so:
urlpatterns = patterns('', # <-- note the `'',`
url(r'^$', views.index, name='index'),
)

Django Generic Views Date-Based URLconf

Trying to teach myself Django but running into a snag.
Generic Views seem to be a great idea but I personally find the documentation a little cryptic at times (maybe I'm being prissy).
So I have been trying to use the Date Based generics views in and specifically ArchieveIndexView.
I have even attempted following some non-djangoproject.com examples and still have problems.
I used the example provided at this site.
Here is my current project/urls.py.
I am also at this point, not worrying about pattern matching, just trying to get it to work.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
With this setup I keep receiving a Invalid Syntax error at the line describing ArchiveIndexView class.
If I comment out this line the problem goes away. If I decouple the URLs to their appropriate app I get the same error.
The error suggest I just have something out of place, a comma or something but I have yet to conclude what it is.
Thank you!
use the below code
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view({'date_field': 'pub_date', 'queryset': Entry.objects.all()})),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
You seem to forget the {} brace required for dict in url(r'^$', ArchiveIndexView.as_view('date_field': 'pub_date', 'queryset': Entry.objects.all())),
line.
Ah. I solved my own question thanks to a little pushing from shiva.
The dictionary works but only for the extra_content argument.
It was just done like that on the website I was trying to copy and for extra content in the documentation so I kept overlooking that glaringly obvious problem.
from django.conf.urls import patterns, include, url
from django.views.generic.dates import ArchiveIndexView
from blog.models import Entry
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^$', ArchiveIndexView.as_view(date_field='pub_date', queryset=Entry.objects.all())),
url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Just needed to sleep on it...

Categories