Reputation: 17
I am new to Django.While practicing. I downloaded HTML template and managed to modify it to load up with django. my issue that i can not get any inputs or outputs from Django<>HTML.I do not know where is the problem.
view.py
from django.shortcuts import render
def home(request):
return render(request, 'home.html' ,{})
def contact(request):
if request.method == "POST" :
username_1 = request.POST["username"]
return render(request, 'home.html', {'username_1' : username_1})
login_page/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
]
home.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>Login V1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
<link rel="icon" type="image/png" href="{%static 'login_page/images/icons/favicon.ico' %}"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/bootstrap/css/bootstrap.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/fonts/font-awesome-4.7.0/css/font-awesome.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/animate/animate.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/css-hamburgers/hamburgers.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/vendor/select2/select2.min.css' %}">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="{%static 'login_page/css/util.css' %}">
<link rel="stylesheet" type="text/css" href="{%static 'login_page/css/main.css' %}">
<!--===============================================================================================-->
</head>
<body>
<div class="limiter">
<div class="container-login100">
<div class="wrap-login100">
<div class="login100-pic js-tilt" data-tilt>
<img src={%static 'login_page/images/img-01.png'%} alt="IMG">
</div>
<form class="login100-form validate-form" action="{% url 'home' %}" method="post" >
{% csrf_token %}
<span class="login100-form-title">
{{ username_1 }}
</span>
<div class="wrap-input100 validate-input" data-validate = "Valid email is required: [email protected]">
<input class="input100" type="text" name="username" placeholder=Username>
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-envelope" aria-hidden="true"></i>
</span>
</div>
<div class="wrap-input100 validate-input" data-validate = "Password is required">
<input class="input100" type="password" name="pass" placeholder="Password">
<span class="focus-input100"></span>
<span class="symbol-input100">
<i class="fa fa-lock" aria-hidden="true"></i>
</span>
</div>
<div class="container-login100-form-btn">
<button class="login100-form-btn">
Login
</button>
</div>
<div class="text-center p-t-136">
<a class="txt2" href="#">
<i class="fa fa-long-arrow-right m-l-5" aria-hidden="true"></i>
</a>
</div>
</form>
</div>
</div>
</div>
<!--===============================================================================================-->
<script src="{%static 'login_page/vendor/jquery/jquery-3.2.1.min.js' %}"></script>
<!--===============================================================================================-->
<script src={%static 'login_page/vendor/bootstrap/js/popper.js' %}"></script>
<script src={%static 'login_page/vendor/bootstrap/js/bootstrap.min.js' %}"></script>
<!--===============================================================================================-->
<script src={%static 'login_page/vendor/select2/select2.min.js' %}"></script>
<!--===============================================================================================-->
<script src={%static 'login_page/vendor/tilt/tilt.jquery.min.js' %}></script>
<script >
$('.js-tilt').tilt({
scale: 1.1
})
</script>
<!--===============================================================================================-->
<script src={%static 'login_page/js/main.js' %}"></script>
</body>
</html>
setting.py
import os
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'dugwc2(qu!9$til@n(gbeur^5bv76m2l&0j(!muv22k+sf$_9@'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'login_page',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'login.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'login.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation
# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]
# Internationalization
# https://docs.djangoproject.com/en/3.0/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
Folders tree : My DJANGO FOLDER TREE SCREEN SHOT
What I am trying to do, is to the username inputs and redirect them to {{ username_1 }} in html.
UPDATE 1 : i modified urls.py and html to the below, and the same issue.
views.
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('', views.home, name="contact"),
]
html
<form class="login100-form validate-form" action="{% url 'contact' %}" method="post" >
{% csrf_token %}
<span class="login100-form-title">
{{ username_1 }}
</span>
Upvotes: 0
Views: 73
Reputation: 522
login_page/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name="home"),
path('contact/', views.contact, name="contact") # Registering the contact function
]
Inside html file
<form class="login100-form validate-form" action="{% url 'contact' %}" method="post" >
passing that name parameter of contact function inside form tag
Upvotes: 2