I'm working through the djangobook tutorials and I have reached the point where I create the hello world view and runserver to see the page when I get a page not found (404 Error).
urls.py code
from django.conf.urls.defaults import *
from mysite.views import *
urlpatterns = patterns('',
(r'^hello/$', hello),
)
views.py code
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
I've copied the code fairly accurately and it should work, if I've missed something please let me know.
Thanks
On trailing slash: https://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#append-slash is a lifesaver.
Related
I just started learning django and running the tutorial part 3, decided to see if I understood the mapping from urls.py to views.py.
I got views and urls to work in the polls app, but then I wanted to make views in the project folder, so I made a views.py file in the project folder (see code below), with a view/function , which I named 'home'.
I then edited the urls.py in the project-folder(see below). run the server, It worked! visiting the url: http://localhost:8000/
it responded:
Hello, world. You're at the HOME PAGE.
BUT.. when I tried to make another view in same views.py called: morn, and adding the url for it, then error (see below),
http://localhost:8000/morn
returning:
localhost refused to connect.
I DID IT EXACTLY THE SAME WAY, so just when I thought I understood it, I didnt get it at all?!?!
The difference between the two views are just their name and path, why doesnt it work then?
on a linux manjaro
Python 3.8.1
Django 3.0.3
#
#this is my urls.py (which I made myself), in the project folder
from django.urls import include, path
from django.contrib import admin
from . import views
#from views import morn
admin.autodiscover()
urlpatterns = [
#lager en index-side
path('', views.home, name='home'),
path('morn/', views.morn, name='morn'),
path('admin/', admin.site.urls, name='admin'),
path('polls/', include('polls.urls')),
]
#
#this is the views.py in myproject folder, same folder as
from django.http import HttpResponse
def home(request):
output = 'Hello, world. You\'re at the HOME PAGE.'
return HttpResponse(output)
def morn(request):
output = 'Hello, world. Youre at the morn-path.'
return HttpResponse(output)
ERROR from konsole running the morn-view:
File "/home/nr1/dev/django/myproject/myproject/urls.py", line 29, in <module>
path('morn/', views.morn, name='morn'),
AttributeError: module 'myproject.views' has no attribute 'morn'
Oh no... I just got it! :)
I have put the views.py file in both of the myproject-folders and the urls.py is just in the inner myproject-folder. I was then updating just the outer views.py-file, when I thought I was in the inner views.py, so, ofcourse, the urls.py couldnt find the new function, it was searching the wrong views.py (outer one).
a lot of frustration for nothing.. but I learnt, I learnt!
I'm learning how to do web programming with Django framework and I got this error message when I try to return a template. When I try to load https://www..../index2/ everything is ok.
This is my urls.py:
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^index2/$', views.index2, name='index2'),
]
This is my views.py:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'main/index.html',)
def index2(request):
return HttpResponse("Hello, world.")
Make sure you have index.html saved under app/templates/main/index.html
if you have debug mode enabled. Exception page will show you the different paths from where django is trying to load index.html.
Thanks.
I added in init.py of my project:
import django
django.setup()
Everything is working correctly.
However I am facing problems whit module urls when I try to import "reverse" from "django.urls". I get the error "No module named urls". I can't understand why I have this error (for the moment I am using "django.core.urlresolvers" to impor "reverse", but I'd like to understand the mistake)
I am using Django 1.11.16 and python 2.7.
Hi I am new to Django framework and I have followed this tutorial https://www.youtube.com/watch?v=qgGIqRFvFFk&list=PL6gx4Cwl9DGBlmzzFcLgDhKTTfNLfX1IK in order to create a website, but when I ran the Music app, it's not executing and in my views.py, there is an error which is showed by underlining the "request" in the "index(request)", that says the parameter 'request' value is not used. Below are the snippets of the codes:
views.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>This is Music App Page")
music/urls.py
from django.http import HttpResponse
def index(request):
return HttpResponse("<h1>This is Music App Page")
website/urls.py
from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^music/', include('music.urls')),
]
In the first image (i.e in Views.py) "request" which is highlighted in red is throwing error stating that parameter 'request' value is not used (unused in scope).
I have followed the exact tutorial as mentioned in the link, can somebody please help? Thanks.!
There is no error here. Your text editor is trying to be helpful by warning you about a style issue, but there is nothing that will prevent your code from running.
There is no error on your code. If you want to get rid of PEP8 warning, you can simply put
# noinspection PyUnusedLocal
on top of your functions
I'm completely new to backend, working through the djangobook tutorial. If I'm missing any vital information, let me know. The first task is to get 'Hello World' to show up on your development server, and it keeps returning 404. The two files in question being the views.py (my hello world file) and urls.py
this is the views.py:
from django.http import HttpResponse
def hello(request):
return HttpResponse("Hello world")
this is the urls.py:
from django.conf.urls import url
from django.contrib import admin
from mysite.views import hello
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^hello/$', hello),
]
I feel like its not finding the views file correctly? This is how there set up, exactly as he said to do it in the tutorial
You need to change the URL's in two places. One in your app like you did, and the other in the django directory. This link has more information on how to do it.
from django.conf.urls import patterns, include, url
urlpatterns = patterns(
'',
url(r'', include('hello.urls')),
)
It was at http://127.0.0.1:8000/hello/, also used HunkDivine's answer as another option
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())