I am trying to make a Django based website, and I want to make a dropdown with if and else conditions working on it, means that if a person selects physics in the dropdown and presses the submit button, it should go to a specific page and if he presses chemistry then it goes to a specific page, please tell me how to do this, I am attaching my code.
Main page (home.html):
<!DOCTYPE html>
<html>
<head>
<title>Hi there</title>
</head>
<body>
<form action="{% url 'test' %}">
<select name="Subject">
<option value="Mathematics">Mathematics</option>
<option value="Physics">Physics</option>
<option value="Chemistry">chemistry</option>
<option value="accounts">accounts</option>
<option value="bsuiness">business</option>
</select>
<input type="submit" value="Generate Password" class="btn btn-primary">
</form>
</body>
</html>
Views.py:
from django.shortcuts import render
def home(request):
return render(request, 'homepage/home.html')
def test(request):
if request.GET.get('Physics'):
return render(request, 'homepage/home.html')
else:
return render(request, 'homepage/test2.html')
urls.py
from django.contrib import admin
from django.urls import path
from homepage import views as home_views
from homepage import views as test_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', home_views.home, name='home'),
path('test/', test_views.test, name='test'),
Please help by telling what to code to make the if statement work
You don't need to add if statement to your django code
Django's backend framework which means you can't do any dinamic change on page.
button generate link
1)https://learn.jquery.com/using-jquery-core/faq/how-do-i-get-the-text-value-of-a-selected-option/
then make more pages and root them
2)https://docs.djangoproject.com/fr/3.1/topics/http/urls/
Before Django please try make a webpage on HTML + CSS + Js
What you want to do is capture the GET/POST request (that you send with your form submit button) in Django, then you get the dropdown parameter and then based on that condition, you can decide which webpage on which url Django will serve.
Then on the client side you have to handle going to the url that you decided to serve with Django.(This can be done with javascript)
Related
I want to execute a python script after a user (registered user) clicks on a button in HTML using Django. How can this be achieved? Can someone give me a hint on how to create the logic in the views.py along with the Html code in the template? Before executing the button code, I want to check whether if the user.id exists in the model. If not, the button doesn't do anything.
Currently, this is basic understanding:
views.py:
def button(request):
#python script
return render(request, 'users/home.html')
home.html:
{% if user.id %}
<button class="btn btn-outline-info" onclick="location.href={% url 'button' %}"> Manual Index </button>
FYI, I am new to Django and am still exploring its potential.
Add your function in urls.py, example code is
from django.contrib import admin
from django.urls import path
from .views import button
urlpatterns = [
.....
path("button/", button, name="button_call"),
.....
]
and simply you can link your button for the url, example code is
{% if user.id %}
<button class="btn btn-outline-info" onclick="location.href={% url 'button_call' %}"> Manual Index </button>
more flexible way to do this is
<button class="btn btn-outline-info"> Manual Index </button>
happy coding
Trying to call Installation function when we click a submit button on HTML Page and taking the data from HTML only,i am new to django.
Help me out..!
Views File:
from .forms import Details
from django import forms
from django.shortcuts import render
from django.http import HttpResponseRedirect
from . import installation
def output(request):
if request.method == 'POST':
details=Details(request.POST)
if details.is_valid():
Obj=details.cleaned_data
path=Obj['Path']
device_type=Obj['Device']
image_version=Obj['Version']
return installation()
else:
raise TypeError
App/urls.py File:
from django.conf.urls import url
from django.contrib import admin
from . import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'',views.output,{'template_name':'home_page.html'}),
]
Forms.py File :
from django import forms
class Details(forms.Form):
Device=forms.CharField(required=True, label='Device_Type', max_length=32)
Path=forms.FileField(required=True, label='Path_to_install_build')
Version=forms.CharField(required=True, label='Version_type',max_length=32)
Where is your HTML? if your frontend (HTML) is not reaching your backend at your views, probabily your action is not right, so check if your form have the right action
<form action="/" method="POST">
{% csrf_token %}
<!-- Rest of the HTML code to your form -->
<input type="submit">Submit</input>
</form>
Im setting you action to / because you using root path to your views, if you change it to like detail, the action should be /detail/
BTW you can use the built-in form from django
{% csrf_token %}
{{ form.as_p }}
<input type="submit">Submit</input>
A frontend developer has given me a front-end layout which consists of html forms. I being a backend developer need to fetch the data from the form and store it in the database. I am using django version 2.0 for the website development. I donot want to use django forms as then I will be forced to make a lot of changes in the html code. How can I extract data from input fields in the HTML forms?
Let us say the we have a simple HTML form in a file index.html
myapp/templates/index.html
In the form, the data must be sent using post method only.
<form action="{% url 'myapp:update' %}" method="post">
<input type="text" name="first_field"/><br>
<input type="text" name="second_field"/><br><br>
<button type="submit" value="Submit"/>
</form>
The purpose of the form is to update the database.
myapp/models.py
from django.db import models
class Person(models.Model):
first_name=models.CharField(max_length=30)
last_name=models.CharField(max_length=30)
myapp/urls.py
from django.urls import path,include
from . import views
app_name='myapp'
urlpatterns = [
path('index/', views.index, name='index'),
path('update/', views.update, name='update'),
]
myapp/views.py
In views.py file, any element in the form can be fetched using request.POST['nameOfTheFieldInTheForm']
from django.shortcuts import render
# Create your views here.
def index(request):
return render(request, 'myapp/index.html', {})
def update(request):
# print('Inside update function')
if request.method=='POST':
# print("Inside post block")
first_field_data=request.POST['first_field']
second_field_data=request.POST['second_field']
x=Person(first_field=first_field_data,
second_field=second_field_data)
x.save()
return index(request)
i want to make simple login system with Django.I want to send a user to a page named 127.0.0.1:8000/login after submitting the form.From then i want to get the post data sent with the request, check if the users exist in the date base and return a html page if he logged in or anothet html page if the user has not logged in.The problem is when i click the submit button of the form, it doesnt make request to /login.It stays at the same page.Any help?
<!DOCTYPE html>
<html>
<body
<form action="/login/" method = "POST">
<input type="text" name="name"><br>
<input type = "submit" value = "Submit">
</form>
</body>
</html>
Urls.py:
from django.conf.urls import url
from django.contrib import admin
from simple import views as f
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^form/$', f.form),
url(r'^login/$', f.data)
]
Views.py
from django.shortcuts import render,render_to_response
from django.http import HttpResponse
from django.template import Context, loader
def form(request):
return render(request,"doccument.html")
def data(request):
name = request.POST.get('name')
return HttpResponse(name)
In your form method should be inside quotes,
<form action="/login/" method="POST">
So I am trying to alter one of the forms from Django-registration: an app I installed via pip.
As stated in the docs, I was to created a registration/registration_form.html and use form as my context:
<html>
<p>This is the registration form</p>
<ul>
{{ form.as_ul}}
</ul>
</html>
So I have 2 questions:
1) How am I to alter the form to have a submit button that actually works in this case?
2) How can I alter the django-registration models so that I can ultimately add more to the registration forms?
Yes I looked over the docs, I am asking here because the language confused me and seemed slightly advance for me.
Thank you!
You need to add the form tags and a submit button. Something like this:
<html>
<p>This is the registration form</p>
<form action="/url/to/register/" method="POST">
{{form.as_ul}}
<input type="submit" value="Register">
</form>
</html>
where "/url/to/register/" will need to be pointed at your view code in your urls.py. Something like this:
from django.conf.urls import url, patterns
from yoursite.registrations import views
urlpatterns = patterns('',
url(r'^url/to/register/', views.register_some_guy),
)