I have a question about urls.py in Django. I am building a blog from scratch as a way of learning Django myself. In the main urls.py file, I have specified the include path to my app's urls.py.
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^', include('myblog.urls')),
)
In the app (called myblog), the urls.py reads as follows:
from django.conf.urls.defaults import *
from models import blogmodel
from django.contrib import admin
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
where getLAtest is the function in my views.py. The error says No module named getLatest
Here's my views.py,
from django.shortcuts import render_to_response
from myblog.models import blogdb
def getLatest(request):
post = blogdb.objects.all()
sorted_post = post.order_by('-served_date')
return render_to_response('blogs.html', {'posts':sorted_post})
Any help is appreciated. Thanks in advance..
You are using the wrong directive; include() is used to include another package; Django will look for a urls.py within the package myblog.views.getLatest when you use that directive.
You want to name the view itself instead:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', 'myblog.views.getLatest'),
)
Note: no include() is being used.
Try updating this:
urlpatterns = patterns('',
(r'^login/', include(admin.site.urls)),
(r'^$', include('myblog.views.getLatest')),
)
to this:
urlpatterns = patterns('',
(r'^getLatest/$', 'myblog.views.getLatest'),
)
include is meant to read in another urls.py file, where you you are wanting to execute a specific view function.
Related
My project urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
My app urls.py is as follows:
from django.conf.urls import patterns, include, url
urlpatterns = patterns('', url(r'^hello/', 'myapp.views.hello', name = 'hello'),)
Now , as soon as I try to run it , it gives me the following error:
from django.conf.urls import patterns, include, url
ImportError: cannot import name 'patterns'
Django doesn't require you to use patterns anymore, you can just make urlpatterns a list of urls
urlpatterns = [url(r'^hello/', 'myapp.views.hello', name = 'hello'),]
Django 1.11 loads that Python module and looks for the variable urlpatterns. This should be a Python list of django.conf.urls.url() instances.
And it runs through each URL pattern, in order, and stops at the first one that matches the requested URL
Dont require patterns more.
urlpatterns should be a Python list of url() instances.
I have a working api, and I am writing a UI to the API, as a separate application in the same project. My project urls.py looks like this
from django.conf.urls import *
import search
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^api/search$', search.validation),
url(r'^ui', include('ui.urls')),
)
My UI app's urls.py looks like this
from django.conf.urls import *
import views
urlpatterns = patterns('',
(r'^ui/$', views.search_template),
)
However, when I am trying to access with my browser(domain.com:8000/ui), I am getting an error.
Using the URLconf defined in api.urls, Django tried these URL patterns, in this order:
^api/search$
^ui ^ui$
The current URL, ui, didn't match any of these.
But if I use the below mapping in the main project's urls.py, it works.
(r'^ui$', ui.views.user_template),
I tried clearing the urls.pyc to make sure it is not stale, but it still persists. Please let me know what am I doing wrong.
You shouldn't repeat ui regex in the app's urls.py:
urlpatterns = patterns('',
(r'^$', views.search_template),
)
I am following the Django tutorial on https://docs.djangoproject.com/en/1.7/intro/tutorial03/, and am trying to get the index view to show up. I've tried the code specified on the page verbatim but keep on getting errors.
polls/urls.py:
from django.conf.urls import patterns, urls
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and finally, the index method in views.py:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def index(request):
return HttpResponse("<h1>Hello world!</h1>");
I'm not sure what I'm doing wrong. I keep getting an error that says "cannot import name 'urls'." any help would be appreciated!
The problem is in your import statement - there is no urls function in django.conf.urls package.
Replace:
from django.conf.urls import patterns, urls
with:
from django.conf.urls import patterns, url
I am running through the Django tutorial.
I have this file
R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test\...
...django_test\article\views.py
Contents:
from django.http import HttpResponse
# Create your views here.
def hello(request):
name = "Mike"
html = "<html><body>Hi %s, this seems to have worked</body></html>" % name
return HttpResponse(html)
And this file:
R:\jeffy\programming\sandbox\python\django_files\tutorial\django_test\...
...django_test\urls.py
Contents:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import hello
urlpatterns = patterns('',
# url(r'^hello/', include(article.views.hello)),
url(r'^admin/', include(admin.site.urls)),
)
I start the Django server
python manage.py runserver
and go to
http://127.0.0.1:8000
And it works:
As does this
http://127.0.0.1:8000/admin
But when I uncomment the "hello" line, it fails:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import hello
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <--Problem line
url(r'^admin/', include(admin.site.urls)),
)
The error says
R:\\jeffy\\programming\\sandbox\\python\\django_files\\tutorial\\django_test
is in the PYTHONPATH, and the "article" folder is in that directory.
Also, why does the import article line not cause an error, but calling the hello function does fail.
Please help me. What am I missing?
UPDATE
No import at all fails the same way (NameError at / name 'article' is not defined):
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <-- Problem line
url(r'^admin/', include(admin.site.urls)),
)
Using import article only, causes this error: AttributeError at / 'module' object has no attribute 'views'
from django.conf.urls import patterns, include, url
from django.contrib import admin
import article
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <-- Problem line
url(r'^admin/', include(admin.site.urls)),
)
Eliminating the import and putting the call in strings (as suggested), results in an ImportError at / No module named 'article.views.hello'; 'article.views' is not a package:
from django.conf.urls import patterns, include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^hello/', include(article.views.hello)), # <-- Problem line
url(r'^admin/', include(admin.site.urls)),
)
And, finally, importing the hello function directly:
from django.conf.urls import patterns, include, url
from django.contrib import admin
from article.views import hello
urlpatterns = patterns('',
url(r'^hello/', include(hello)),
url(r'^admin/', include(admin.site.urls)),
)
With this, calling http://127.0.0.1:8000 fails but in an expected way:
But then, http://127.0.0.1:8000/hello fails with this:
There's a few things that are wrong here.
1) You are importing something you are not using:
from article.views import hello
...
url(r'^hello/', include(article.views.hello)),
You import hello and then call it from the module article, but article has not been imported and so will be undefined. You can either import article or you can call hello directly.
2) include is not necessary here.
We normally use include for including other urls.py schemas. hello is a function. The way that url-routing works in Django is that you define a route and pass it a function, like so:
from article.views import hello
urlpatterns = patterns('',
url(r'^hello/', hello),
url(r'^admin/', include(admin.site.urls)),
)
I have a site made in PHP that i need to create a django site of. I've stripped out the PHP code temporary (not much code anyways), but i'm having problems understanding how django works and how to create a simple template to display a page.
I know there's thousands of books and guides out there, but most of them go too deep or doesn't do what i need. I just need two simple pages, page1 and page2, which will be accessed through domain.com/page1 and domain.com/page2.
What is the simplest way to achieve that?
This is what i have in my urls.py file so far, is that correct at least?
from django.conf.urls.defaults import patterns, include, url
urlpatterns = patterns('',
url(r'^$', 'mysite.views.page1', name='home'),
url(r'^$page2', 'mysite.views.page2', name='page2'),
)
It obviously doesn't work now cause the views aren't created.
Any help is greatly appreciated,
// qwerty
I recommend you to walk through tutorial, you will find out everything Django beginner should know.
Try this:
from django.conf.urls.defaults import patterns, include, url
from mysite.yourapp import views
urlpatterns = patterns('',
url(r'^$', 'mysite.views.page1', name='home'),
url(r'^page2/$', 'mysite.views.page2', name='page2'),
)
The r'^page1/$ bit is python regex
in your views.py file define your views:
def page1:
#something
This should help you get started http://docs.djangoproject.com/en/1.3/intro/tutorial01/
Well, i found the most straight-forward way is:
1) urls.py
from django.urls import include, path
from . import views
from django.views.generic import TemplateView
urlpatterns = [
path('test.html', TemplateView.as_view(template_name='main/test.html')),
]
2) templates/test.html
Hello world!
Classic 3-steps:
1) urls.py
from django.urls import include, path
from . import views
urlpatterns = [
path('test.html', views.test, name='test'),]
2) views.py
from django.shortcuts import render
def test(request):
return render(request, 'test.html')
3) templates/test.html
Hello world!
Thats more or less what I use:
urlpatterns = patterns('',
(r'^$', 'news.views.page1'),
(r'^page2/$', 'news.views.page2'),
)
just for your understanding: the beginning of a line is expressed as ^, the end as $. So ^$ stands for an empty line. more about regexp: http://docs.python.org/library/re.html
If you are using older django than direct_to_template generic view is what you need.
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^page1/$', direct_to_template, {'template': 'page1.html'}),
(r'^page2/$', direct_to_template, {'template': 'page2.html'}),
)
Or for newer django 1.3 you need to use class based generic views
from django.conf.urls.defaults import *
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^page1/', TemplateView.as_view(template_name="page1.html")),
(r'^page2/', TemplateView.as_view(template_name="page2.html")),
)
P.S. Don't forget to create page1.html and page2.html template files.