Django Tutorial 3 - URL producing 404 error - python

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/.

Related

Page not found 404 - Django

I'm a newbie to Django and I know this probably has been asked alot of times.
So basically what's happening is when I try to create a new project and whenever I'm trying to run my server, by default it's opening http://127.0.0.1:8000/catalog/ and not http://127.0.0.1:8000/.
Even if I run the server with my other projects, I'm facing the same error.
I followed this django basics tutorial on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Django/skeleton_website
Idk but somehow I think it's default address is set to http://127.0.0.1:8000/catalog/.
Here's the link to the repo for the project:
https://github.com/Fanceh/django-404-error
Here's my project's urls.py:
from django.contrib import admin
from django.urls import path, include
from testuapp import urls
urlpatterns = [
path('admin/', admin.site.urls),
path('',include("testuapp.urls"))
]
Here's the code in my testuapp urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('', views.testu),
]
Here's my webapp's views.py file:
from django.shortcuts import render
# Create your views here.
def testu(request):
render(request, 'Greetings!')
Is there any way I can change it?
Regards
From the tutorial link it mentions the redirect.
Any request for the root URL, will redirect you to /catalog.
Screenshot from the tutorial below.
HTH
so i assume you are below url pattern structure in your testuapp project.
urlpatterns = [
path('catalog',include("views.catalog"))
]
views.calalog is the name of the method in your view file.
Ok I think I figured it out, it's just the chrome cache. I cleared it and bam it's working!

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: reverse function fails with an exception

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/.

Categories