Django 1.11:Apps aren't loaded yet - python

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.

Related

django server is showing no result

I started working on django ( read the django documentation and several youtube tutorials )
I dont understand what is wrong , the editor is not showing any errors ( VSCode ).
When I start the server it is showing me the default rocket icon ....I have tried reinstalling django but it also didnt help....also restarted the server several times.
I am stuck right now.
main url file
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('calc/',include('calc.urls')),
path('admin/', admin.site.urls),
]
url file for the app (calc)
from django.urls import path
from . import views
urlpatterns = [
path('',views.index,name='index')
]
views file for the app
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return HttpResponse("Hello World")
edit url path in calc app to the below.
path('', views.home ,name='index')

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.

ModuleNotFoundError: No module named 'src'

I'm changing view of homepage with app names pages.
I've added pages to settings. this is how directory look like:
- trydjango
- src/
- pages/
- __init__
- views
- products
- trydjango/
- __init__
- settings
- urls
- manage
views' code:
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home_view(*args, **kwargs):
return HttpResponse("<h1>Hello Again</h1>")
urls code
from django.contrib import admin
from django.urls import path
from src.pages.views import home_view
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_view, name='home'),
]
and I see this error when I run server
ModuleNotFoundError: No module named 'src'
First you need to understand what an app in Django is compared to a project.
When you register an app django will look in the project root folder when you try to import it.
Your project root is where your manage.py file is. In your case the src folder.
So when you want to import your views module you need to state
from pages.views
rather than
from src.pages.views
I suggest that you read through and follow (by coding it yourself) the Django tutorial to learn more about project structure and creating your own apps with models, urls etc.
I got the same problem, IDE may use red underline but this code is still correct:
from pages.views

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

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

Categories

Resources