I am working on PythonAnywhere and made my first project. I am trying to access many views (HTML pages) from my views.py. Here is my project tree:
emailpro
--emailpro
----url.py
--emailapp
----urls.py
----views.py
--templates
----emailapp
------index.html
------help.html
Code inside my emailpro>emailpro>urls.py is
from django.conf.urls import url, include
from django.contrib import admin
#from emailapp import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
#url(r'^$', views.index, name='index'),
url('', include('emailapp.urls')),
]
Code inside my emailpro>emailapp>urls.py is:
from django.conf.urls import url
from emailapp import views
urlpatterns=[
url('', views.index, name='index'),
url(r'^help/$', views.help, name='help'),
]
Code inside my emailpro>emailapp>views.py is
from django.shortcuts import render
#from django.http import HttpResponse
# Create your views here.
def index(request):
my_dict={'insert_me': "Hi I am index coming from views.py"}
return render(request, 'emailapp/index.html', context=my_dict)
def help(request):
help_dict={'help_insert': "HELP PAGE"}
return render(request, 'emailapp/help.html', context=help_dict)
My emailpro>templates>emailapp>index.html is
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Index</title>
</head>
<body>
<h1>Index page</h1>
{{ insert_me }}
</body>
</html>
Code inside my emailpro>templates>emailapp>help.html is
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title> Help </title>
</head>
<body>
<h1>This is my Help HTML</h1>
{{ help_insert }}
</body>
</html>
Now when I run my site and access http://fatimatestpro.pythonanywhere.com/, it shows me my index.html page which means it is going inside my emailpro>emailapp>urls.py to get its view from views.py but when I try to access http://fatimatestpro.pythonanywhere.com/help/, it shows the same page as index.html.
Please help me to send it to different view.
Thanks in advance
your index url is matching before it reaches the help url.
try:
urlpatterns=[
url(r'^help/$', views.help, name='help'),
url('/', views.index, name='index'),
]
Related
Running into a max recursion error when hitting my static main.css file in django. Need help identifying the issue and how to correct it.
Had no issues when my login/sign up pages were within my single dbManagementApp. After adding those to a new app (accounts) then adding the templates into a specific folder for each app, I received the below error message.
Debugger
In template ###, error at line 7
maximum recursion depth exceeded while calling a Python object
1 <!DOCTYPE html>
2 <html lang="en" dir="ltr">
3 <head>
4 <meta charset="utf-8">
5 <title>HOME</title>
6 {% load static %} <!-- load the static folder for css-->
7 <link rel="stylesheet" href="{% static 'main.css' %}">
Traceback
File "C:\Users\lukep\anaconda3\envs\djangoenv\lib\site-packages\django\template\base.py", line 766, in __init__
self.literal = float(var)
ValueError: could not convert string to float: "'main.css'"
My main urls.py:
from django.contrib import admin
from django.urls import path, include
from dbManagementApp import dbmviews
from accounts import accviews
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('dbManagementApp.dbmurls')),
path('accounts/', include('accounts.accurls'))
]
dbManagementApp/dbmurls.py
from django.urls import path
from . import dbmviews
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', dbmviews.home, name='home'),
path('aboutus/', dbmviews.aboutUs, name='aboutUs'),
path('pricing/', dbmviews.pricing, name='pricing'),
path('contact/', dbmviews.contact, name='contact'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
dbManagementApp/dbmviews.py
from django.shortcuts import render, redirect
from django.views import View
from .models import Doc
from .forms import DocumentForm
from django.contrib import messages
from django.http import HttpResponse
from django.http import JsonResponse
# Create your views here.
def home(request):
return render(request, 'dbmapp/index.html')
templates/dbmapp/index.html
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>HOME</title>
{% load static %} <!-- load the static folder for css-->
<link rel="stylesheet" href="{% static 'main.css' %}">
accounts/accurls.py
from django.urls import path
from . import accviews
urlpatterns = [
path('register/', accviews.register, name='register'),
path('login/', accviews.login, name='login')
]
accounts/accviews.py
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect('login')
else:
form = UserCreationForm()
return render(request, 'registration/register.html', {'form':form})
def login(request):
return render(request, 'registration/login.html')
I'm not fully understand your problem, but I'd change 3 things
{% load static %} should be at the top of the html file
Move path('', include('dbManagementApp.dbmurls')), first (before admin)
Remove dir attribute in html.
Just a question, are you using {% extends 'whatever.html' %}?, it cause error from duplicating tags.
error is when http://127.0.0.1:8000/about
Page not found (404)
Request Method: GET
Request URL: http://127.0.0.1:8000/about
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
The current path, about, 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.
projets
mysite urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('mainapp.urls')),
]
main app urls.py
from django.urls import path,include
from . import views
urlpatterns = [
path('',views.home),
path('about',views.about),
path('contact',views.contact)
]
views.py
from django.shortcuts import render,HttpResponse
def home(request):
return render(request,"index.html")
def about(request):
return render(request,"about.html")
def contact(request):
return render(request,"contact.html")
templates/about.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document1</title>
</head>
<body>
<h1>Mahabir pun</h1>
<p>he is a great person he is a doing grat</p>
</body>
how to solve please help me i am in trouble for 2 days
Have you added 'mainapp' to 'INSTALLED_APPS' in your project's 'settings.py'?
try adding the slashes
path('about/',views.about),
path('contact/',views.contact)
and then you can try
http://127.0.0.1:8000/about/
Your URL Patterns does not have a name inside the path. Do the following changes and let me know.
from django.urls import path,include
from . import views
urlpatterns = [
path('',views.home, name='home'),
path('about',views.about, name='about'),
path('contact',views.contact, name='contact')
]
The name inside the path is link name which should also be used in the anchor href. e.g. for the home page link and so on for the other pages links too. Thanks
I am following this tutorial: https://realpython.com/get-started-with-django-1/.
I followed everything exactly the same but when I run manage.py, localhost:8000 only displays a blank webpage. I have a very simple HTML file that should display "Hello, World!" but it's not loading.
Here is the code in settings.py:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'hello_world',
views.py:
from django.shortcuts import render
# Create your views here.
def hello_world(request):
return render(request, 'hello_world.html', {})
My hello_world.html file:
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<h1>Hello, World!</h1>
</head>
<body>
</body>
</html>
The code in my project "personal_portfolio" personal_portfolio\urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('hello_world.urls')),
]
And finally hello_world\urls.py:
from django.urls import path
from hello_world import views
urlpatterns = {
path('', views.hello_world, name = 'hello_world'),
}
I've made sure all the files are in the right place, but I can't figure out why localhost:8000 only displays a white page. When I run the hello_world.html file locally, it works just fine. Like this:
local HTML file
Any tips would be much appreciated. I'm frustrated I can't get this to work.
Your app url patterns are defined as a dictionary and should be a list.
So in hello_world/urls.py, change this;
urlpatterns = {
path('', views.hello_world, name = 'hello_world'),
}
To be a list of paths;
urlpatterns = [
path('', views.hello_world, name='hello_world'),
]
You also have your <h1> tag in the head, not the body. You need to use a <title> tag in the head & move the <h1>;
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hello, World!</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
The form action is not redirecting from home.html to password.html in Django 2 even I recheck everything including URL pattern
Below I am sharing the basic code. My apologies if it's a basic question as I am very new to Django that's why I may not able to detect the issue.
urls.py code
from django.urls import path
from generator import views
urlpatterns = [
path('', views.home),
path('password/', views.password),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request, 'generator/home.html')
def password(request):
return render(request, 'generator/password.html')
home.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Home</title>
</head>
<body>
<h1>Password Generator</h1>
<form action="password" method="get">
<select name="length">
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
</select>
<input type="button" value="Generate Password">
</form>
</body>
</html>
password.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password</title>
</head>
<body>
<h1>Password page</h1>
</body>
</html>
Error Log
File Structure
First of all give names in urls.py so you can access it by name.
urlpatterns = [
path('', views.home,name="index"),
path('password/', views.password,name="password"),
]
in home.html remove form's action
in views.py
from django.urls import reverse
def home(request):
if request.method == 'POST':
#you can access input items of form by `request.POST.get('attribute_name')`
# your logic
return redirect(reverse('password')
else:
return render(request, 'generator/home.html')
if still not getting error then please share whole code and what you want to achieve
Hello there i m looking to fix a problem i m working on django right now the issue is that when i try to go to the admin area login it returns me to the same index.html page
my code:
urls.py on main folder
from django.conf.urls import url,include
from django.contrib import admin
from first_app import views
urlpatterns = [
url('',views.index,name="index"),
url('first_app/',include('first_app.urls')),
url('admin/', admin.site.urls)
]
urls.py on first_app folder
urlpatterns = [
url('',views.index,name="index")
]
the views.py on first_app folder
def index(request):
my_dic = {'insert_me':"Hello Jinja"}
return render(request,'index.html',my_dic)
index.html file
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{% static 'css/style.css'%}">
<title>Django Page</title>
</head>
<body>
<p>{{ insert_me}}</p>
<h1>a picture </h1>
</body>
</html>
You are mixing up path with url, if you are using Django < 2.0, then use url with regex, like this:
from django.conf.urls import include, url
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^first_app/', include('first_app.urls')),
url(r'^*$', views.index, name='index'),
...
]
And if you are using Django >= 2.0, then use like this:
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('first_app/',include('first_app.urls')),
path('',views.index,name="index"),
]
More information can be found in documentation.