Django Tutorial Part 3 - Template - python

I am new to Django and Python. So please excuse if my question is trivial.
This is in reference to Django Docs, tutorial part 3
In Section Decoupling the URLConfs, the authors says to to have the following in mysite/urls.py:
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
and the below code to be part of polls/urls.py
from django.conf.urls import patterns, include, url
urlpatterns = patterns('polls.views',
url(r'^$', 'index'),
url(r'^(?P<poll_id>\d+)/$', 'detail'),
url(r'^(?P<poll_id>\d+)/results/$', 'results'),
url(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I HAVE BEEN WADING THRU THIS TUTORIAL BY HAVING THE URLS.PY IN Polls DIRECTORY.
IS THERE MYSITE DIRECTORY AS WELL AS POLLS DIRECTORY THAT HAS URLS.PY?
Thanks in Advance

One of the things about django is that it prides itself on its ability to have apps that are pluggable and modular. The urls.py inside the mysite directory is your PROJECT wide url conf.
This line
url(r'^polls/', include('polls.urls')),
tells your PROJECT that you have an app/directory/python package that has a urls.py file. In this file which lives in mysite/polls/urls.py you will have the url conf for the polls APPLICATION that will live at /polls/whatever in your browser.

I'm new in python. Coincidentally I just went through the entire tutorial by chance a few hours ago. Your urls.py should only be located in /mysite/mysite/ directory.
This is where my urls.py is located
/home/mattrising/desktop/mysite/mysite#
Hope that helps :-)
Edit: your polls directory should NOT contain urls.py file by default.

Related

Circular Import in Django

I have been trying to run a client-server application using Django. When I am trying to run my server in Django , it is giving me the following error.
django.core.exceptions.ImproperlyConfigured: The included URLconf '' does not appear to
have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
The project urls.py -
from django.conf.urls import url,include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('chat.views')),
]
App's views.py -
from django.shortcuts import render
from django.http import JsonResponse
def home(request):
if request.method == 'POST':
if request.is_ajax():
//code
return JsonResponse(data)
return render(request,'index.html')
Where am I going wrong?
include method takes app urls.py model not views.py. You need to create urls.py file inside your app and replace url(r'^', include('chat.views')) with url(r'^', include('chat.urls')) in projects urls file. See django docs.
Include method in url.py file is used to include url patterns that are specified in other file. and when you are doing this url(r'^', include('chat.views')), it is unable to find url patterns in your views file. Hence giving error this:
django.core.exceptions.ImproperlyConfigured: The included URLconf ''
does not appear to have any patterns in it. If you see valid patterns
in the file then the issue is probably caused by a circular import.
we generally create a urls.py file in our app folder and write all our url patterns regarding this app in this file.
create a new urls.py file in your app folder and write url patterns in that file.
and then include your app's urls.py file in main urls.py file like this:-
url(r'^', include('chat.urls')),
and your app's urls.py file should look like:
from django.conf.urls import url
urlpatterns = [
url(r'', views.home, name = "home")),
]
you can find out more about django urls from documentation:- django urls
And if you don't want to create new urls.py file in you app directory then you can just import your views in main urls.py file and write url pattern in this file. then your main urls.py file will look like this:-
from django.conf.urls import url,include
from django.contrib import admin
from chat.views import home
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', home, name = "home"),
]

django urlconf 404 on webroot

Django: 1.8.5
Python: 3.5
I'm getting a 404 on just the webroot. I don't know what's wrong with my urlconf.
Directory structure:
myproject // from django-admin startproject
myproject // '' ''
__init__.py
settings.py
urls.py
views.py
myapp // from manage.py startapp
migrations
__init__.py
admin.py
models.py
tests.py
urls.py
views.py
In myproject > urls.py:
from django.conf.urls import include, url, patterns
from myapp import urls as app_urls
urlpatterns = patterns('',
url(r'^/$', include(app_urls)),
)
And in myapp > urls.py:
from django.conf.urls import include, url, patterns
urlpatterns = patterns('',
url(r'^/$', 'index'),
)
I run the django development server and I get a 404 on the index page, even though I have the empty regex defined.
Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:
^/$
The current URL, , didn't match any of these.
The documentation in urls.conf from a a fresh install of django:
Including another URLconf
1. Add an import: from blog import urls as blog_urls
2. Add a URL to urlpatterns: url(r'^blog/', include(blog_urls))
Changing the regex in BOTH urls.py files to r'^' appears to work, but with a new 'Could not import 'index'. error.
Don't use $, when using include. Change it to:
# myproject/urls.py
from django.conf.urls import include, url, patterns
from myapp import urls as app_urls
urlpatterns = patterns('',
url(r'^', include(app_urls)),
)
# myapp/urls.py
from django.conf.urls import include, url, patterns
urlpatterns = patterns('',
url(r'^$', 'index'),
)
The root URL is ^$, not ^/$.
Also, when you include a urlconf from another urlconf, it uses both patterns together; it doesn't forget the original pattern. So your app is really looking for the url ^/$^/$, which I'm not sure can ever be matched.

Django urls.py import not detecting

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

The included urlconf doesn't have any patterns in it + django

I have been trying out the django tutorial and stuck with this error. I am in the 3rd page of the tutorial and everything was going good until i encountered this error Django Tutorial. I was following the exact steps in the tutorial. This is the error i got
ImproperlyConfigured at /polls
The included urlconf <module 'polls.urls' from 'C:\\Python34\\mysite\\polls\\urls.py'> doesn't have any patterns in it
I am pasting my code here.
ROOT_URLCONF = 'mysite.urls'` in settings.py,
from django.conf.urls import patterns, url
from polls import views
urlpattern = patterns('',
url(r'^$',views.index,name='index')
)`
inside MYSITE/POLLS/URLS.PY
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'mysite.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
INSIDE MYSITE.URLS.PY
Sorry if i missed out anything that i had to mention to get a clear picture. Any help would be appreciated.
The first file has urlpattern instead of urlpatterns.

Django - How to use URLconfs with an apps folder?

I'm following the tutorial on the Django website but I'm trying to expand upon it. I like the organizational scheme of putting all of your apps in an "apps" folder. I'm trying to figure out the proper way to include urls.py in order to get everything to link together.
Here's my root urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('apps.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
Here's my urls.py at apps/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns=patterns('polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
)
What's the correct way to do this? Thanks!
The way you currently have it set up... the URLs for polls would be:
http://your.url.here/polls/polls/235/results/
This is probably not what you want. The include function in the urlpatterns in the root urls.py file specifies "polls/" as a prefix to all urlpatterns in the polls app. Therefore, in the polls/urls.py file, you shouldn't specify the "polls/" prefix again as it will cause duplicate prefixes.
How are you running your Django instances? If you have multiple vhosts configured in Apache then each Django instance in /apps has it's own urls.py.
I got it to work by doing this:
urlpatterns=patterns('polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
I guess the polls part is taken care of in the root urlconf

Categories

Resources