Value is not used (unused in scope) in Django Framework - python

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

Related

How do I map urls to views in Django, getting AttributeError: module 'myproject.views' has no attribute

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!

Django Template Language Not Rendering

I am brand new to Django and following along with the tutorial. I'm hoping this is just an obvious mistake, but I am unable to get my web browser to render anything written in the Django template language and I can't figure out why.
Here is my directory structure for some context: https://imgur.com/dGNIiDa
project/urls.py
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('budget/', include('budget.urls')),
path('admin/', admin.site.urls)
]
budget/urls.py:
from django.urls import path
from . import views
urlpatterns = [
path('<int:account_id>/', views.get_account, name='detail'),
]
budget/views.py:
from django.shortcuts import render
from django.http import HttpResponse
from budget.models import Account, Transaction
def get_account(request, account_id):
accts = Account.objects.filter(pk=account_id)
context = {"test": accts}
return render(request, 'budget/detail.html', context)
budget/templates/budget/detail.html:
<p>This is a {{ context.test }}</p>
When I visit localhost:8000/budget/1 in my browser this is all that is rendered: https://imgur.com/j2Vh0yb
Clearly Django is finding the template file and sending it to the browser, but anything that is written inside of {} does not get recognized or rendered at all. I've followed along exactly with the tutorial and I have no idea why it's not working. Any ideas?
You don't need context in the expression in the template; all that you put in the context are "globals" in the template, so try
<p>This is a {{ test }}</p>
instead.
Django's template engine has the unfortunate property of being silent about nonexisting properties, so it's hard to debug stuff like this.

Django 1.11:Apps aren't loaded yet

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.

Django Custom error page's giving error

I'm trying to make custom error page general 404 and 500.
I'm not trying to raise just making a default if it happens for a user, but every place I'm trying to search and follows a tutorial and so on I always ends up in getting an internal error on both 500 and 404
I'm running django=1.11.6, and I'm running debug False because I need to see if it work of course.
How can I fix this issue and make it work?
And how can I make it so it also gives the user the error text so they can send it to me?
Views.py (In my app folder)
# Error Handlers
def handler404(request):
return render(request, 'handlers/404.html', status=404)
def handler500(request):
return render(request, 'handlers/500.html', status=500)
Urls.py (in my main config folder)
from django.conf.urls import include, url, handler404, handler500
from django.contrib import admin
handler404 = 'staff.views.handler404'
handler500 = 'staff.views.handler500'
urlpatterns = [
url(r'^', include('staff.urls')),
url(r'^admin/', admin.site.urls),
]
Firstly, you don't need custom handlers if you just want to use your own templates - just put your own 404.html and 500.html files in your root templates directory.
Secondly, rather than getting users to send you the error codes manually, you can configure Django to send you errors by email.

404 Error when creating a helloworld page using django

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.

Categories

Resources