django urls.py regex isn't working - python

This is for Django 1.2.5 and Python 2.7 on Wamp Server running apache version 2.2.17.
My problem is that the my URLConf in urls.py isn't redirecting, it's just throwing a 404 error.
urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
#from django.contrib import admin
#admin.autodiscover()
urlpatterns = patterns('',
(r'^app/$', include('app.views.index')),
# Uncomment the admin/doc line below to enable admin documentation:
#(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
#(r'^admin/', include(admin.site.urls)),
)
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World")
I'm getting the following error:
ImportError at /app/
No module named index
I'm stumped as I'm only learning Django, can anybody see something wrong with my code? Here's my PythonPath:
['C:\Windows\system32\python27.zip', 'C:\Python27\Lib', 'C:\Python27\DLLs', 'C:\Python27\Lib\lib-tk', 'C:\wamp\bin\apache\Apache2.2.17', 'C:\wamp\bin\apache\apache2.2.17\bin', 'C:\Python27', 'C:\Python27\lib\site-packages', 'c:\wamp\www\seetwo']

Yes, your url should look like this:
(r'^app/$', 'app.views.index'),
Using the include statement means you are pointing to a new urlconf file!
http://docs.djangoproject.com/en/dev/topics/http/urls/#including-other-urlconfs
So it's looking for a module index which doesn't exist (as opposed to a function if you didn't use include())

Related

Django adding url.py with Django-batchimport 404 error

I have created a very basic Djangodb where I am trying to do a batch import of several thousand excel files that I have. In the process of researching the best way to database all of these files I found this: https://github.com/pstch/django-batchimport
I have read through the installation documentations, and on the last step of adding the urls to the url.py file I keep getting a 404 error for both my localhost/admin and localhost/batchimport.
Here is the code for url.py
from django.conf.urls import include, url, patterns
from django.contrib import admin
from batchimport import *
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'export.views.home', name='home'),
# url(r'^blog/', include('blog.urls')),
(r'^admin/', include(admin.site.urls)),
(r'^batchimport', include('batchimport.urls')),
)
and here is my code for urls.py in my batchimport folder.
from django.conf.urls import *
from views import ImportUploadView, ImportOptionsView, ImportRunView
urlpatterns = patterns('',
url(r'^upload/$',
ImportUploadView.as_view(),
name='batchimport_upload'),
url(r'^options/$',
ImportOptionsView.as_view(),
name='batchimport_options'),
url(r'^run/$',
ImportRunView.as_view(),
name='batchimport_run'), )
Here is the output from the error that I am getting:
ImportError at /admin/ cannot import name related
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.8
Exception Type: ImportError
Exception Value: cannot import name related
Exception Location: /Users/USER/Development/ExportOCC/export/batchimport/utils.py
in <module>, line 5 Python
Executable: /Users/USER/anaconda/bin/python Python
Version: 2.7.9
The import is not available in Django 1.8. You can give it a try and import related from the original source. Change batchimport/utils.py like:
from django.db.models import get_model
from django.db.models.fields import AutoField, related
If that works, please also notify the author of django-batchimport!

Create a view in django

Very new to django. I'm using version 1.5.2 and I just did a fresh install. I'm using the django development server; I'll be moving to Apache down the road, but I want to understand the django's particular flavor of MVC methodology before doing taking that step.
So I start up the django server with `python manage.py runserver 0.0.0.0:8000' through the terminal in my project directory (django_books). I get this error:
ViewDoesNotExist at /
Could not import django_books.views.home. Parent module django_books.views does not exist.
So my view doesn't exist. My view.py file is empty because the tutorial I was following did not include one. I'm not sure if this is the problem. If it is, how do I create this file (what goes in it)?
Directory Structure:
django_books
beer (from the tutorial lol)
migrations
__init__.py
models.py
views.py
random_book
(same as beer above)
django_books (this is my actual django project, beer and random_book are apps)
__init__.py
settings.py
urls.py
wsgi.py
media
.gitignore
manage.py
requirements.txt (output from pip freeze command)
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('',
# Examples:
url(r'^$', 'django_books.views.home', name='home'),
# url(r'^django_books/', include('django_books.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
If you keep your urls.py the way it is, that means you need to create views.py within /django_books/django_books/
Within that file, create a new function called home.
Alternately, if you have any functions inside of /django_books/beer/, you could reference them from urls.py.
All urls.py does is expose a python path to a function and route a URL there. So you can see that you don't have a module or file called views within django_books/django_books, which is why you get the failure.
View is basically a python function that receives HTTP Request and returns HTTP Response.
Quote from docs:
A view function, or view for short, is simply a Python function that
takes a Web request and returns a Web response. This response can be
the HTML contents of a Web page, or a redirect, or a 404 error, or an
XML document, or an image . . . or anything, really. The view itself
contains whatever arbitrary logic is necessary to return that
response. This code can live anywhere you want, as long as it’s on
your Python path. There’s no other requirement–no “magic,” so to
speak. For the sake of putting the code somewhere, the convention is
to put views in a file called views.py, placed in your project or
application directory.
This line url(r'^$', 'django_books.views.home', name='home'), in urls.py points the index / of your site to the home view - you should create it.
Create a python function called home in views.py:
from django.http import HttpResponse
import datetime
def home(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)
Restart your development server and visit http://127.0.0.1:8000.
FYI, read the tutorial more carefully, part 3 is about dealing with urls and views.

404 when enabling admin with Django

I had correctly configured a Django app and had it running smoothly. However, when I went to enable the administration panel, I encountered a 404 error upon uncommenting this line:
url(r'^admin/', include(admin.site.urls))
Upon trying to access my site, I received this:
Using the URLconf defined in cms.urls, Django tried these URL patterns, in this order:
1. ^admin/
The current URL, , didn't match any of these.
I can't figure out how to get around this error. Does anyone with some Django experience have a solution? Thanks!
EDIT: Here is the entire urls.py file:
from django.conf.urls.defaults import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'cms.views.home', name='home'),
# url(r'^cms/', include('cms.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Have you tried, http://yourdomain:port/admin ? If you are using the default url, I would try
http://127.0.0.1:8000/admin

Does Django cache url regex patterns somehow?

I'm a Django newbie who needs help: Even though I change some urls in my urls.py I keep on getting the same error message from Django. Here is the relevant line from my settings.py:
ROOT_URLCONF = 'mydjango.urls'
Here is my urls.py:
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^mydjango/', include('mydjango.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
#(r'^admin/doc/', include(django.contrib.admindocs.urls)),
# (r'^polls/', include('mydjango.polls.urls')),
(r'^$', 'mydjango.polls.views.homepage'),
(r'^polls/$', 'mydjango.polls.views.index'),
(r'^polls/(?P<poll_id>\d+)/$', 'mydjango.polls.views.detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'mydjango.polls.views.results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'mydjango.polls.views.vote'),
(r'^polls/randomTest1/', 'mydjango.polls.views.randomTest1'),
(r'^admin/', include(admin.site.urls)),
)
So I expect that whenever I visit http://mydjango.yafz.org/polls/randomTest1/ the mydjango.polls.views.randomTest1 function should run because in my polls/views.py I have the relevant function:
def randomTest1(request):
# mainText = request.POST['mainText']
return HttpResponse("Default random test")
However I keep on getting the following error message:
Page not found (404)
Request Method: GET
Request URL: http://mydjango.yafz.org/polls/randomTest1
Using the URLconf defined in mydjango.urls, Django tried these URL patterns, in this order:
1. ^$
2. ^polls/$
3. ^polls/(?P<poll_id>\d+)/$
4. ^polls/(?P<poll_id>\d+)/results/$
5. ^polls/(?P<poll_id>\d+)/vote/$
6. ^admin/
7. ^polls/randomTest/$
The current URL, polls/randomTest1, didn't match any of these.
I'm surprised because again and again I check urls.py and there is no
^polls/randomTest/$
in it, but there is
^polls/randomTest1/'
It seems like Django is somehow storing the previous contents of urls.py and I just don't know how to make my latest changes effective.
Any ideas? Why do I keep on seeing some old version of regexes when I try to load that page even though I changed my urls.py?
Django compiles the URL regexes when it starts up for performance reasons - restart your server and you should see the new URL working correctly.

Why does Django throw an exception whenever I enable admin.autodiscover()?

Here is my setup. I am using Django version 1.1.1 on Dreamhost, Python 2.4. The problem I am having is whenever I create a simple app and also have admin.autodiscover() enabled, Django will throw an exception. My setup:
from django.conf.urls.defaults import *
from testapp.views import HelloWorld
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^HelloWorld/$', HelloWorld),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
)
My settings.py looks like:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.admindocs',
'testapp',
)
My testapp.views looks like:
from django.http import HttpResponse
def HelloWorld(request):
return HttpResponse("Hello world")
If I comment out admin.autodiscover() I can get to my view of HelloWorld. If I enable admin.autodiscover() Django throws an exception that I am not able to trap.
Does anyone know why this might be happening and what I can do to fix it?
I've had a similar issue when I've renamed an app. Basically, if you've launched the app and used the admin using admin.autodiscover() in your urls.py file, it will cause an admin.pyc file to be created in your app folder. Delete this admin.pyc file and run the server again...and voila!
I'm going to guess that testapp/admin.py does not import the models.Model class you are creating admin for. Try the following:
./manage.py shell # you may immediately get a stack trace
>> import testapp.admin # I'll bet it blows up.
Old question, but still relevant. I had a similar problem that stumped me today. The issue was that I had refactored a set of files in a directory within a Django app folder (lib/cache) into a single file (lib/cache.py). Because there was still an __init__.py in the cache directory, Python was seeing the empty directory as a module and preventing access to cache.py.
Commenting out admin.autodiscover() in my urls.py made it a bit easier to track this down but it still took some guesswork.

Categories

Resources