.I am new to django. please help me with the solution.I want to get specific event name and number at a particular page. It has to same id and same name on next page. But, the input is showing me the last value of database.eventlist.html
<form method="post">
{% csrf_token %}
<label for="">Event Name</label>
<input type="text" name="eventname">
<input type="submit">
</form>
my view.py
def eventlist(request):
if request.method == 'POST':
eventname = request.POST.get('eventname')
eventid = str(random.randint(100000000, 999999999))
eventlistdata = EventList(eventname = eventname,eventid=eventid)
eventlistdata.save()
return render(request,"adminsys/eventlistlist.html",{'eventname': eventname,'eventid':eventid})
return render(request, 'adminsys/eventlist.html')
my eventlistlist.html file only for passing values
<form method="post">
{% csrf_token %}
<table>
<tr>
<th>Event Name</th>
<th>Event ID</th>
<th>Submit</th>
</tr>
{% for post in post %}
<tr>
<td><input type="text" name="eventname" id="eventname" value="{{post.eventname}}"></td>
<td><input type="text" name="eventid" id="eventid" value="{{post.eventid}}"></td>
<td><button type="submit">Submit</button></td>
</tr>
{%endfor%}
</table>
</form>
my views.py of eventlistlist file
<form method="post">
{% csrf_token %}
<label for="">Event Name</label>
<input type="text" value="{{eventname}}" id="eventn">
<label for="">Event Id</label>
<input type="text" value="{{eventid}}" id="eventi">
</form>
And the last page where I want to get my same value as given in eventlistlist.html
<form method="post">
{% csrf_token %}
<label for="">Event Name</label>
<input type="text" value="{{eventname}}" id="eventn">
<label for="">Event Id</label>
<input type="text" value="{{eventid}}" id="eventi">
</form>
I am always getting the data which is last in my mysql database.
I will attach the images below for reference
When I will select the first options:
I am receiving the last value in my datadabe
Related
I'm trying to get values from an object of my DB on a form in html to edit it but when I call the current "value" from the attribute I get this:
Form
HTML
<form enctype="multipart/form-data" method="post">
{% csrf_token %}
{% for campo in formulario %}
<div class="mb-3">
<label for="" class="form-label">{{ campo.label }}:</label>
{% if campo.field.widget.input_type == 'file' and campo.value %}
<br/>
<img src="{{MEDIA_URL}}/imagenes/{{campo.value}}" width="100"srcset="">
{% endif %}
<input
type="{{ campo.field.widget.input_type}}"
class="form-control"
name="{{ campo.name }}"
id=""
aria-describedby="helpId"
placeholder="{{campo.label}}"
value="{{campo.value | default:''}}"
/>
</div>
<div class="col-12 help-text"> {{campo.errors}}</div>
{% endfor %}
<input name="" id="" class="btn btn-success" type="submit" value="Enviar informacion">
</form>
Views
Models
I found the problem... I instantiate the class instead of the object
View.py
if req.method == 'POST':
df = DocumentForm.objects.filter(document_id=id)
logging.info('LOG: I am here')
if df.exists():
for d in df:
description = req.POST['{}'.format(d.description, False)]
displayName = req.POST['{}'.format(d.displayName, False)]
df.update(displayName=displayName, description=description)
return redirect('/adboard/upd')
HTML File
<form class="needs-validation" action="{{id}}" method="POST" enctype="multipart/form-data" novalidate>
{% csrf_token %}
<div class="row mt-3 mb-3"></div>
{% if messages %}
<div class="alert alert-danger" role="alert">
{% for message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
{% for df in data %}
<div class="form-group">
<small class="text-muted bold-label m-lower">{{{df.field}}}</small>
<label for="validationCustom01">{{df.displayName}}</label>
<input type="text" class="form-control" id="validationCustom01" name="{{df.displayName}}" value="{{df.displayName}}" placeholder="Enter Display Name">
<label for="validationCustom01">{{df.description}}</label>
<input type="text" class="form-control" id="validationCustom01" name="{{df.description}}" value="{{df.description}}" placeholder="Enter description">
<div class="invalid-feedback">
Please enter required fileds.
</div>
<!-- <div class="valid-feedback">
Looks good!
</div> -->
</div>
{% endfor %}
<button type="submit" class="btn btn-primary btn-red btn-block">SAVE</button>
</form>
What I am trying to achieve is.
Pass a variable form to the user to fill
And I get the result.
I can't tell what the input id/name would be because it's a variable.
But I am finding it difficult getting the value from the view.py file.
change
req.POST['{}'.format(d.description, False)]
to
req.POST.get('{}'.format(d.description, False))
I need to create a form with django to check if students are prensent or not. I can't use the 'normal' way to create a form because django want a form model implemented with every fields composing the form. I can't do this cause i don't know the number of students in a class. how can i create a dynamic form who don't care about the number of students ?
at the moemnt i created a form in the template with a for to showthe list of students with each checkbox
<form method="post">
{% csrf_token %}
Date : <input id="date" type="date" class="mt-3 mb-3">
<br>
{% for student in student_list %}
<input type="checkbox" id="student{{forloop.counter}}" name="student{{forloop.counter}}">
<label for="student{{forloop.counter}}">{{student.first_name }} {{student.last_name}}</label>
<br>
{% endfor %}
<button class="btn btn-outline-primary mr-4">Cancel</button>
<button type="submit" class="btn btn-success radius d-flex">Submit</button>
</form>
This might not be the best practice, but I think it should work.
form.html
<form method="POST">
{% csrf_token %}
Date : <input id="date" type="date" class="mt-3 mb-3">
<br>
{% for student in student_list %}
<input type="checkbox" id="student" name="student" value="1">
<label for="student">{{ student.first_name }} {{ student.last_name }}</label>
<br>
{% endfor %}
<button class="btn btn-outline-primary mr-4">Cancel</button>
<button type="submit" class="btn btn-success radius d-flex">Submit</button>
</form>
views.py
def submit_form(request)
if request.method == 'POST':
date = request.POST.get("date")
student = request.POST.getlist("student")
# TODO
EDIT :
I just named each checkbox 'student' then i get each checkbox data in view.py
call_of_roll.html
<form method="post">
{% csrf_token %}
Date : <input name="date" type="date" class="mt-3 mb-3" value="{{defaultdate|date:"Y-m-d" }}" required>
<br>
{% for student in student_list %}
<input type="checkbox" id="{{student.id}}" name="student{{student.id}}" checked>
<label for="student{{student.id}}">{{student.first_name }} {{student.last_name}}</label>
<br>
{% endfor %}
<p>Check if the student is present</p>
<div class="row">
<button class="btn btn-outline-primary mr-4">Cancel</button>
<button type="submit" class="btn btn-success radius d-flex">Submit</button>
</div>
</form>
views.py
if request.method == 'POST':
date = request.POST.get("date")
for student in students:
missing = request.POST.get('student'+str(student.id), "off")
I have an app which reads the questions from database and display them .I need to enter the response to it and save it back in the database.Is it possible to do it without using forms
My HTML section
<form id="survey" method="post" action="/Survey/restricted/" class="form col-md-12 center-block">
{% csrf_token %}
{% for b in obj1 %}
<textarea class="form-control">{{b.question}}</textarea>
<textarea class="form-control" rows="3" name="response1" width="100%;">{{b.response}}</textarea>
{% endfor %}
<input type="submit" name="submit" class="btn btn-success" value="Submit Response" />
</form>
I tried to access the data after entering the response and submitting using
res=request.POST.get('response1')
but it returns none
You should move the response1 field outside the loop, but then it should be passed as a separate field (e.g. response), not as part of the obj1 list of questions (or not passed at all if keeping the previous answer is unnecessary).
<form id="survey" method="post" action="/Survey/restricted/" class="form col-md-12 center-block">
{% csrf_token %}
{% for b in obj1 %}
<textarea class="form-control">{{b.question}}</textarea>
{% endfor %}
<textarea class="form-control" rows="3" name="response1" width="100%;">{{response}}</textarea>
<input type="submit" name="submit" class="btn btn-success" value="Submit Response" />
</form>
Also, I think the questions should not be editable (i.e. disabled, or not a textarea altogether)
The URL is not changing and data is not fetching after user login.
After Login the url not changed(i expected to change to 127.0.0.1:8000/employer/home)
#login_required
def home(request):
emp=Employer.objects.get(user=request.user)
jb=Job.objects.filter(employer=emp).order_by("-postdate")
return render(request,'employer/home.html',{'jobs':jb})#,{'employer':emp})
employer/home.html
<table class="table table-hover table-bordered">
<tr><th>Job ID</th><th>Title</th><th>Posted on</th></tr>
{% for job in jobs %}
<tr>
<td>{{ job.pk }}</td>
<td>{{ job.title }}</td>
<td>{{ job.postdate }}</td>
</tr>
{% endfor %}
</table>
login.html
<form class="form-signin" action="" method="post">{% csrf_token %}
<h4 class="form-signin-heading">Sign in to my account...</h4>
{% if errors %}
<p class="text-error">Sorry, that's not a valid username or password</p>
{% endif %}
<input type="text" id="username" class="input-block-level" name="username" placeholder="UserID">
<input type="password" id="password" class="input-block-level" name="password" placeholder="Password">
<p style="text-align:right;">Forgot your password?</p>
<button class="btn btn-large btn-success" type="submit" value="login">Sign in</button>
</form>
You need to tell it where to go after login using the next url parameter:
<form class="form-signin" action="{% url django.contrib.auth.views.login %}?next=employer/home/" method="post">
or set the LOGIN_REDIRECT_URL in settings:
The URL where requests are redirected after login when the contrib.auth.login view gets no next parameter.