Django: reverse function fails with an exception - python

I'm following the Django tutorial and got stuck with an error at part 4 of the tutorial. I got to the part where I'm writing the vote view, which uses reverse to redirect to another view. For some reason, reverse fails with the following exception:
import() argument 1 must be string, not instancemethod
Currently my project's urls.py looks like this:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/(.*)', include(admin.site.root)),
)
and the app urls.py is:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'details'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
And the vote view is: (I've simplified it to have only the row with the error)
def vote(request, poll_id):
return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(1,)))
When I remove the admin urls include from the project's urls.py, i.e. making it into:
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
#(r'^admin/(.*)', include(admin.site.root)),
)
it works.
I've tried so many things and can't understand what I'm doing wrong.

The way you include the admin URLs has changed a few times over the last couple of versions. It's likely that you are using the wrong instructions for the version of Django you have installed.
If you are using the current trunk - ie not an official release - then the documentation at http://docs.djangoproject.com/en/dev/ is correct.
However, if you are using 1.0.2 then you should follow the link at the top of the page to http://docs.djangoproject.com/en/1.0/.

Related

Django cant be reached via parameterized url

Hello StackOverflow community,
I'm currently learning how to use the library Django combined with Python. However, I've ran into some issues which are somehow strange. My situation is the following. I have a project called "animals" which is the base of the Django application. My app is called "polls". Then I've defined the following views.
polls/views.py
from django.shortcuts import render
from django.http import HttpResponse
def animals(request, animal_id):
return HttpResponse("%s" % animal_id)
def index(request):
return HttpResponse('Hello world')
So far, so good. Unfortunatly I can't say the same about the urlpatterns.
polls/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index'),
# Redirects to localhost:8000/polls/<animal_id>
path('<int:animal_id>/',views.animals,name='animals')
]
Whenever I want to navigate to the url "localhost:8000/polls/10/" Django reminds me of the fact, that the url is not accepted by the application and a 404 Error is thrown inside my browser. Am I missing something here?
UPDATE
I've managed to resolve the problem by fixing a rather trivial error. While the polls/urls.py was alright, the problem lay inside the animals/urls.py file. This file looked like this:
animals/urls.py
from django.conf.urls import url
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
url(r'^admin/', admin.site.urls),
path('polls',include('polls.urls')),
]
As one can see, the "polls" path is not finished with a "/" sign. This indicates to Django that my desired route would be localhost:8000/polls where is any integer I add to the url. However, you have to add the slash at the end. Otherwise Django won't work as expected.

No module named 'django_simple.todo'

I am working through a (mycodesmells) django tutorial and towards the bottom under "ADDING TO THE PROJECT" it says to update the url.py file with the code that it gives.
Once I update that file it crashes the server and gives me the error.
"No module named 'django_simple.todo'
I looked in SO posts and template view and redirect were mentioned in the following post.
1. Does this mean its deprecated?
2. How do i fix or adjust the code for Django 1.1 and Python 3
Templateview
from django.conf.urls import include, url
from django.contrib import admin
from django_simple.todo import views as todo_views
admin.autodiscover()
urlpatterns = [
url(r'^$', todo_views.index),
url(r'^admin/', include(admin.site.urls)),
]
Make sure you have an __init__.py file in the "todo" folder. That lets Python know it's a module.
Also make sure you have django_simple.todo in your Django apps list in your settings.py.

Issue with Django Web Tutorial

I am extremely new to programming. I searched and found a few similar threads here, though unfortunately the answers in those threads didn't seem to fix my own issue.
Problem
I am working my way through the Django tutorials, and I am on the third tutorial:
https://docs.djangoproject.com/en/1.7/intro/tutorial03/
I'm not sure if this matters, but I am working on a Macintosh w/ Mavericks.
The root directory in which I am working is 'Test2'. The directory structure is:
/Users/me/src/collact/collact/test2/
Within 'test2', and per the previous two tutorials, I have '/polls' and '/templates' folders.
My polls/views.py file has the following text:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the polls index.")
My polls/urls.py file has the following text:
from django.conf.urls import patterns, url
from polls import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
Finally, in /test2/, I have a urls python file as well (.py). It has the text:
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)),
)
As far as I can tell, this is what the tutorial prescribes. Unfortunately, when I run the server using: python manage.py runserver, the website gives me an error/404 message:
Page not found (404)
Request Method:
GET Request
URL: http://localhost:8000/polls/ Using the URLconf defined in
test2.urls, Django tried these URL patterns, in this order: ^admin/
The current URL, polls/, 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.
Does anyone have any idea how I can fix this? I know that this is a really stupid question, but I have been trying to figure it out for the past day or two, and unfortunately I'm just not familiar enough with programming to solve this on my own.

Django Tutorial 3 - URL producing 404 error

I am following the Django 1.6 tutorial 3 (https://docs.djangoproject.com/en/1.6/intro/tutorial03/) and I have become stuck towards the beginning - "Writing your first view".
The Django tutorial first says to edit the views.py, which I have done as follows:
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello, world. You're at the poll index.")
Then it says to create a urls.py in the same directory, which I have done. My urls.py looks like this:
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)),
)
The next instruction is to now check the URL http://localhost:8000/polls/ for the message 'Hello, world. You're at the poll index.' but my browser simply produces the Django 404 error - Page not found.
I'm sure I'm doing something pretty stupid but I can't what is wrong, I've tried restarting the python webserver. I've followed the previous 2 tutorials with success, can anybody help me out?
If it makes a difference, I'm using Windows7, Python 3.4.1 and Django 1.6.5
Your view.py for polls must be in projectname/polls/ directory when first urls.py will be in projectname/projectname/.
Then, if you decide to configure and add more polls app specific urls, you can create urls.py in projectname/polls/.

Django urls.py template issue

I am new to Django and just playing around with the code and features but I seem to be stuck (yes I have done the entire tutorial). For some reason my mysite/urls.py is not taking me to the right urls, actually it ONLY takes me to one template no matter what I do. 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('app.views',
(r"^app/$",'index'),
(r"^app/detail/$",'detail'),
url(r'^admin/', include(admin.site.urls)),
)
My app/views.py is:
from django.core.urlresolvers import reverse
from django.shortcuts import render_to_response
from django.http import HttpResponse
from mysite.app.models import Post,PostAdmin
def index(request):
return render_to_response("app/link1.html")
def detail(request):
return HttpResponse("You're looking at detail.")
I have a base.html template and link1 which {% extends base.html %}. But for some reason if I enter http://localhost:8000/app or http://localhost:8000/app/detail, I get the link1.html? Or even when I do http://localhost:8000/, I just get the link1.html. What could cause this? (Its probably something minor)
If I understand correctly shouldn't the url pattern with (r^"app/detail/$",'detail'), take me to the detail HttpResponse in views.py like the tutorial rather than a template?
Thank you
I have checked your code, it works just fine. I cannot replicate such error with your views and urls. Post your templates and directory structure to make sure they are fine too.
test result for /app/
Hello link 1.
test result for /app/detail/
You're looking at detail.

Categories