Django displays source code instead of web page - html

I am new to django. Any help will be appreciated. It displays source code instead of web page. To be specific the base page (base.html). What I want is to use the data from patient details and doctors detail as they are from different groups. I think that the problem is when i pass my dictionary.
Views.py
def booking(request):
if not request.user.is_active:
messages.success(
request, ("In order to book an appointment you must login first"))
return redirect('login')
doctor_details = Doctor.objects.all()
f = {'doctor_details': doctor_details}
g = request.user.groups.all()[0].name
if g == 'Patient':
patient_details = Patient.objects.all().filter(EmailAddress=request.user)
d = {'patient_details': patient_details}
return render(request, 'booking.html', f, d)
Html
{% extends 'base.html' %}
{% block content %}
{% load static %}
<div class="loginContainer">
<div class="img">
<img src="{% static 'image/booking.svg' %}">
</div>
<div class="login-content">
{% for d in patient_details %}
<form method="POST" id="signupForm">
{% for f in doctors_details %}
{% csrf_token %}
<h2 class="title">Booking form</h2>
<div class="input-div one">
<div class="i">
<ion-icon name="person-circle"></ion-icon>
</div>
<div class="div">
<h5>Patient ID: PID - {{d.id}}</h5>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="person"></ion-icon>
</div>
<div class="div">
<h5>Name: {{d.FirstName}} {{d.LastName}}</h5>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="business"></ion-icon>
</div>
<div class="div">
<h5>Department</h5>
<input type="text" class="input" name="age" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="medkit"></ion-icon>
</div>
<div class="div">
<h5>{{f.name}}</h5>
<input type="text" class="input" name="age" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="bandage"></ion-icon>
</div>
<div class="div">
<h5>Symptoms</h5>
<input type="textarea" class="input" name="address" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="document-text"></ion-icon>
</div>
<div class="div">
<h5>Comments (Optional)</h5>
<input type="textarea" class="input" name="address">
</div>
</div>
<button type="submit" class="loginBtn">Submit</button>
Return to Personal Profile
Cancel Booking
{% endfor %}
</form>
{% endfor %}
</div>
</div>
{% ifequal error 'no' %}
<script type="text/javascript">
alert("You have successfully registered.")
window.location = ('loginPage')
</script>
{% endifequal %}
{% ifequal error 'yes' %}
<script type="text/javascript">
alert("Something went wrong.")
</script>
{% endifequal %}
{% endblock %}
Urls.py
from django.urls import path
from . import views
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('', views.index),
path('contact', views.contact),
path('loginPage', views.loginPage, name='login'),
path('doctorlogin', views.doctorlogin, name='doclogin'),
path('help', views.help),
path('signup', views.signup, name='signup'),
path('doctors', views.doctors, name='doctors'),
path('booking', views.booking, name='booking'),
path('userProfile', views.userProfile, name='userProfile'),
path('doctorProfile', views.doctorProfile, name='doctorProfile'),
path('logout', views.logout, name='logout')
]
urlpatterns = urlpatterns + \
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

You should pass context as one dictionary. You can use update method to add new items to your dictionary:
def booking(request):
if not request.user.is_active:
messages.success(
request, ("In order to book an appointment you must login first"))
return redirect('login')
doctor_details = Doctor.objects.all()
f = {'doctor_details': doctor_details}
g = request.user.groups.all()[0].name
if g == 'Patient':
patient_details = Patient.objects.all().filter(EmailAddress=request.user)
f.update({'patient_details': patient_details}) # Using update
return render(request, 'booking.html', f)

Thank you everyone for contributing. I found my mistake. The problem was in the way i use the views.py. The above answer is correct but i prefer to use it this way.
Views.py
def booking(request):
if not request.user.is_active:
messages.success(
request, ("In order to book an appointment you must login first"))
return redirect('login')
doctor_details = Doctor.objects.all()
g = request.user.groups.all()[0].name
if g == 'Patient':
patient_details = Patient.objects.all().filter(EmailAddress=request.user)
d = {'patient_details': patient_details,
'doctor_details': doctor_details} #edited part
return render(request, 'booking.html', d)
And for the html part.
Html
{% extends 'base.html' %}
{% block content %}
{% load static %}
<div class="loginContainer">
<div class="img">
<img src="{% static 'image/booking.svg' %}">
</div>
<div class="login-content">
<form method="POST" id="signupForm">
{% csrf_token %}
<h2 class="title">Booking form</h2>
{% for f in patient_details%} #here is the change
<div class="input-div one">
<div class="i">
<ion-icon name="person-circle"></ion-icon>
</div>
<div class="div">
<h5>Patient ID: PID - {{f.id}}</h5>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="person"></ion-icon>
</div>
<div class="div">
<h5>Name: {{f.FirstName}} {{f.LastName}}</h5>
</div>
</div>
{% endfor %}
<div class="input-div one">
<div class="i">
<ion-icon name="business"></ion-icon>
</div>
<div class="div">
<h5>Department</h5>
<input type="text" class="input" name="age" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="medkit"></ion-icon>
</div>
<div class="div">
<div class="custom-select">
<select name="doctors" required>
{% for f in doctor_details %} #here is the change
<option value="{{f.name}}">{{f.name}}</option>
{% endfor %}
</select>
</div>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="bandage"></ion-icon>
</div>
<div class="div">
<h5>Symptoms</h5>
<input type="textarea" class="input" name="address" required>
</div>
</div>
<div class="input-div one">
<div class="i">
<ion-icon name="document-text"></ion-icon>
</div>
<div class="div">
<h5>Comments (Optional)</h5>
<input type="textarea" class="input" name="address">
</div>
</div>
<button type="submit" class="loginBtn">Submit</button>
Return to Personal Profile
Cancel Booking
</form>
</div>
</div>
{% ifequal error 'no' %}
<script type="text/javascript">
alert("You have successfully registered.")
window.location = ('loginPage')
</script>
{% endifequal %}
{% ifequal error 'yes' %}
<script type="text/javascript">
alert("Something went wrong.")
</script>
{% endifequal %}
{% endblock %}

Related

define an element as default value in a list created by a for loop in a django template

here is my html
<div class="modal-body">
<form action="#" id="periodeOptionForm">
<div class=" card-body">
<div class="form-group form-elements">
<div class="form-label">Options</div>
<div class="custom-controls-stacked">
{% for periode in periodes %}
<label class="custom-control custom-radio" id'{{periode.labelle}}'>
<input type="radio" class="custom-control-input" name="example-radios" value="{{periode.valeur}}">
<span class="custom-control-label">{{periode.labelle}} </span>
</label>
{% endfor %}
</div>
</div>
</div>
</form>
</div>
My fbv
def periodes(request):
...
periodes = [{"labelle":"10 minutes","valeur":10},{"labelle":"30 minutes","valeur":30},{"labelle":"1 heure","valeur":60}]
....
return render(request,"my_template.html",{"periodes":periodes}
When I display the html page no elements are selected. I want the first element of my periodes list to be selected by default. How can I do it?
You can use forloop.first around checked:
<input type="radio" class="custom-control-input" name="example-radios" value="{{periode.valeur}}" {% if forloop.first %}checked{% endif %}>

Django Formset one form in each row

How to place each form of a formset on a horizontal line, one below the other without using a table?
My html is putting everything on one line.
Any help much appreciated.
Html
[<div class="form-group row">
<div class="row">
{{ formset.management_form }}
{% for form in formset.forms %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
{% endfor %}
</div>
</div>
image from screen
This reorganization solved the problem.
Thanks for the help!
<div class="form-group row">
<div class="col-sm-12">
{{ formset.management_form }}
{% for form in formset %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
<div class="row">
<div class="col-sm-2">
{{ form.logical_operator|as_crispy_field }}
</div>
<div class="col-sm-3">
{{ form.event|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.operator|as_crispy_field }}
</div>
<div class="col-sm-2">
{{ form.event_value|as_crispy_field }}
</div>
<div class="col-md-1 align-self-center delete-form">
<button onclick="exclude(this)" type="button"
title="Excluir"
class="btn btn-danger btn-sm">
<i class="feather icon-trash-2"></i>
</button>
</div>
</div>
{% endfor %}
</div>
</div>
Please try using </br> (break line) command:
<h1> HEllo</h1> </br> <h2>How are you</h2>

Checkboxes are not the same height as input boxes - Bootstrap

I want the checkboxes on the left side to be the same height as the text input boxes on the right.
HTML:
{% block content %}
<h1>{{ls.name}}</h1>
<form method="post" action="#">
{% csrf_token %}
{% for item in ls.item_set.all %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<div class="input-group-text">
{% if item.complete == True %}
<input type="checkbox", value="clicked", name="c{{item.id}}" checked>
{% else %}
<input type="checkbox", value="clicked", name="c{{item.id}}">
{% endif %}
</div>
</div>
<input type="text", value="{{item.text}}" class="form-control">
</div>
{% endfor %}
<div class="input-group mb-3">
<div class="input-group-prepend">
<button type="submit", name = "newItem", value="newItem" class="btn btn-primary">Add Item</button>
</div>
<input type="text", name="new">
</div>
<button type="submit", name = "save", value="save" class="btn btn-primary">Save</button>
</form>
{% endblock %}
give you check box class then in you linked css give checkbox height and width acc to your need
<div class="input-group-text">
{% if item.complete == True %}
<input class="check" type="checkbox", value="clicked", name="c{{item.id}}" checked>
{% else %}
<input class="check" type="checkbox", value="clicked", name="c{{item.id}}">
{% endif %}
then in your css
.check {
height:20px;
width:20px;
}

Post comments in Django Homepage

i have this code with me as part of a tutorial i was following for blog post. I have a comment section on my detailview, I want it to be shown in the homepage as well. I tried with some codes, but the problem is same comments are showing in all posts (only) in homepage.
please take a look at the code.
I have a comment snippet which I use it in my homepage.
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{{ comments.count }} Comment{{ comments|pluralize }}
{% for comment in comments %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
Delete</td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>
PS. The comment is correctly showing in the detail view, but in homepage its showing all the comments of all posts in every posts.
Have the following changes,
views.py
...
comments = Comment.objects.filter(post=posts_list.first()) # Delete this line
context = {
'page':page,
'posts':posts,
'tag':tag,
'actions':actions,
'comments': comments, # Delete this too
}
return render(request, 'posts/users/myhome.html', context)
Change posts/comment_section.html too,
<!-- Comment showing section -->
<div class="main-comment-section">
<div class="container-fluid mt-2">
<div class="form-group row">
<!-- Comment Form -->
<form class="comment-form" method="post" action=".">{% csrf_token %}
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<button type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">Comment</button>
</form>
<!-- Comment Form end -->
</div>
</div>
{% if not post.comment_set.all %}
No comments to display.
{% endif %}
{% for comment in post.comment_set.all %}
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="10" width="10" src="{{ comment.user.profile.profile_pic.url }}"><h6>{{ comment.user.first_name|capfirst }} {{ comment.user.last_name|capfirst }}</h6><br>
<p style="font-size: 8px;">{{ comment.timestamp }}</p>
<p style="font-size: 14px;" class="mb-3">{{ comment.content }}</p>
<a type="button" name="button" class="reply-btn ml-4"><p style="font-size: 13px;"> Reply</p></a>
{% if request.user == comment.user %}
Delete</td>
{% endif %}
</blockquote>
{{ comment.reply.count }}
<div class="replied-comments col-md-5" style="display: none;">
{% for reply in comment.replies.all %} <!--replies is the related name in the model-->
<blockquote class="blockquote">
<img style="float:left; clear: left;" class="rounded-circle article-img" height="50" width="50" src="{{ reply.user.profile.profile_pic.url }}"><h6>{{ reply.user.first_name|capfirst }} {{ reply.user.last_name|capfirst }}</h6><br>
<p style="font-size: 13px;" class="mb-3">{{ reply.content }}</p>
</blockquote>
{% endfor %}
<div class="form-group row">
<form class="reply-form" method="post" action=".">{% csrf_token %}
<input type="hidden" name="comment_id" value="{{ comment.id }}">
<div class="form-group">
<textarea name="content" cols="60" rows="2" maxlength="1000" required="" id="id_content"></textarea>
</div>
<input type="submit" value="submit" class="btn-sm btn-outline-light" style="color: black;">
</form>
</div>
</div>
{% endfor %}
</div>

Bootstrap misaligned label under textarea

I have a bootstrap form which consists of basic input fields and a few textarea's. Im using Django for my form validation and am having a persistent problem aligning validation labels under the textarea's. This form is only using bootstrap css modules so there is no custom css interfering with it as far as i can tell.
Example: The date and time field align perfectly as the notes validation label does not.
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="well well-lg">
<form class="form-horizontal" action="" autocomplete="off" method="post">
{% csrf_token %}
{% for field in UserForm %}
<div class="form-group">
<label class="col-md-4 control-label" for="{{ field.label }}"> {{ field.label }}</label>
<div class="col-md-6">
{{ field }}
</div>
{% for error in field.errors %}
<div class="col-md-6">
<strong>{{ error|escape }}</strong>
</div>
{% endfor %}
</div>
{% endfor %}
</form>
</div>
</div>
</div>
The number of columns inside the class="row" does not add up to 12. Try rewriting the form-group part like this:
<div class="form-group">
<label class="col-md-4 control-label" for="{{ field.label }}"> {{ field.label }}</label>
<div class="col-md-6">
{{ field }}
{% for error in field.errors %}
<p>
<strong>{{ error|escape }}</strong>
</p>
{% endfor %}
</div>
</div>