who can explain me why my SearchView doesn't work. I have some code like this.It doesn't show me any mistakes, but it doesn't work. The page is clear. Seems like it doesn't see the input.
search.html
<div class="justify-content-center mb-3">
<div class="row">
<div class="col-md-8 offset-2">
<form action="{% url 'search' %}" method="get">
<div class="input-group">
<input type="text" name="q" class="form-control" placeholder="Search..." />
<div class="input-group-append">
<button class="btn btn-dark" type="submit" id="button-addon2">Search</button>
</div>
</div>
</form>
</div>
</div>
</div>
search/urls.py
path('search/', SearchView.as_view(), name='search')
search/views.py
class SearchView(ListView):
model = Question
template_name = 'forum/question_list.html'
def get_queryset(self):
query = self.request.GET.get("q")
object_list = Question.objects.filter(
Q(title__icontains=query) | Q(detail__icontains=query)
)
return object_list
forum/question_list.html
{% extends 'main/base.html' %}
{% block content %}
{% for question in object_list %}
<div class="card mb-3">
<div class="card-body">
<h4 class="card-title">{{ question.title }}</h4>
<p class="card-text">{{ question.detail }}</p>
<p>
{{ question.user.username }}
5 answers
10 comments
</p>
</div>
</div>
{% endfor %}
{% endblock %}
I want to hide the field form.name_of_parent_if_minor if the age (CharField) < 18 else show. I am not getting where to write jQuery or JS code or add separate js file. For this do I need to the html definition and add tag for age (CharField) or we can perform action on this as well.
I am new to the Django so if you find any mistakes in my code then any help would be appreciated for guidance.
forms.py
class StudentDetailsForm(forms.ModelForm):
class Meta:
model = StudentDetails
views.py
class StudentCreateView(LoginRequiredMixin, CreateView):
template_name = 'student/student_details_form.html'
model = StudentDetails
form_class = StudentDetailsForm
success_url = "/"
html
{% extends 'student/base.html' %}
{% load crispy_forms_tags %}
{% block content %}
<div id="idParentAccordian" class="content-section">
<form method="POST">
{% csrf_token %}
<div class="card mt-3">
<div class="card-header">
<a class="collapsed card-link" style="display: block;" data-toggle="collapse"
href="#idPersonalInformation">Personal Information</a>
</div>
<div id="idPersonalInformation" class="collapse show" data-parent="#idParentAccordian">
<div class="card-body">
<div class="row">
<div class="col-6">
{{ form.joining_date|as_crispy_field }}
</div>
<div class="col-6">
{{ form.class|as_crispy_field }}
</div>
</div>
{{ form.student_name|as_crispy_field }}
{{ form.father_name|as_crispy_field }}
{{ form.mother_name|as_crispy_field }}
{{ form.gender|as_crispy_field }}
<div class="row">
<div class="col-6">
{{ form.date_of_birth|as_crispy_field }}
</div>
<div class="col-6">
{{ form.age|as_crispy_field }}
</div>
</div>
<div>
{{ form.name_of_parent_if_minor|as_crispy_field }}
</div>
</div>
</div>
</div>
<div class="form-group mt-3">
<button class="btn btn-outline-info" type="submit">Save</button>
<a class="btn btn-outline-secondary" href="javascript:history.go(-1)">Cancel</a>
</div>
</form>
</div>
{% endblock content %}
the easiest way but not the cleanest is to add JS script in your html
the recommanded way is to add static folder to your app and load static files in your html template using {% load static %}
In the below form If I uncomment "Product" field my form is not working.I think there is something wrong with my html file. I am not including whole model code only the neccessary code.
lead/models.py
class Lead(models.Model):
description = models.TextField(blank=True, null=True)
assigned_to = models.ManyToManyField(User, related_name='lead_assigned_users')
account_name = models.CharField(max_length=255, null=True, blank=True)
tags = models.ManyToManyField(Tags, blank=True)
contacts = models.ManyToManyField(Contact, related_name="lead_contacts")
ctype = models.CharField(default=None,max_length=20, choices=TYPE, blank=True, null=True)
product = models.CharField(default=None,max_length=255, blank=False, null=True)
common/models.py
class Products(models.Model):
name = models.CharField(max_length=50,null=False,blank=False)
def __str__(self):
return self.name
lead/view.py
#login_required
def create_lead(request):
template_name = "create_lead.html"
if request.POST:
form = LeadForm(request.POST, request.FILES, assigned_to=users)
if form.is_valid():
lead_obj = form.save(commit=False)
lead_obj.created_by = request.user
lead_obj.save()
if request.POST.get('tags', ''):
tags = request.POST.get("tags")
splitted_tags = tags.split(",")
for t in splitted_tags:
tag = Tags.objects.filter(name=t)
if tag:
tag = tag[0]
else:
tag = Tags.objects.create(name=t)
lead_obj.tags.add(tag)
if request.POST.getlist('assigned_to', []):
lead_obj.assigned_to.add(*request.POST.getlist('assigned_to'))
assigned_to_list = request.POST.getlist('assigned_to')
if request.POST.getlist('products', []):
lead_obj.product.add(*request.POST.getlist('products'))
products = request.POST.getlist('products')
if request.POST.getlist('teams', []):
user_ids = Teams.objects.filter(id__in=request.POST.getlist('teams')).values_list('users', flat=True)
assinged_to_users_ids = lead_obj.assigned_to.all().values_list('id', flat=True)
for user_id in user_ids:
if user_id not in assinged_to_users_ids:
lead_obj.assigned_to.add(user_id)
current_site = get_current_site(request)
recipients = list(lead_obj.assigned_to.all().values_list('id', flat=True))
send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain,
protocol=request.scheme)
if request.FILES.get('lead_attachment'):
attachment = Attachments()
attachment.created_by = request.user
attachment.file_name = request.FILES.get(
'lead_attachment').name
attachment.lead = lead_obj
attachment.attachment = request.FILES.get('lead_attachment')
attachment.save()
if request.POST.get('status') == "converted":
account_object = Account.objects.create(
created_by=request.user, name=lead_obj.account_name,
email=lead_obj.email, phone=lead_obj.phone,
description=request.POST.get('description'),
website=request.POST.get('website'),
)
account_object.billing_address_line = lead_obj.address_line
account_object.billing_street = lead_obj.street
account_object.billing_city = lead_obj.city
account_object.billing_state = lead_obj.state
account_object.billing_postcode = lead_obj.postcode
account_object.billing_country = lead_obj.country
for tag in lead_obj.tags.all():
account_object.tags.add(tag)
if request.POST.getlist('assigned_to', []):
# account_object.assigned_to.add(*request.POST.getlist('assigned_to'))
assigned_to_list = request.POST.getlist('assigned_to')
current_site = get_current_site(request)
recipients = assigned_to_list
send_email_to_assigned_user.delay(recipients, lead_obj.id, domain=current_site.domain,
protocol=request.scheme)
account_object.save()
success_url = reverse('leads:list')
if request.POST.get("savenewform"):
success_url = reverse("leads:add_lead")
return JsonResponse({'error': False, 'success_url': success_url})
return JsonResponse({'error': True, 'errors': form.errors})
context = {}
context["lead_form"] = form
context["accounts"] = Account.objects.filter(status="open")
context["users"] = users
context["countries"] = COUNTRIES
context["status"] = LEAD_STATUS
context["source"] = LEAD_SOURCE
context["ctype"] = TYPE
context["product"] = Products.objects.all()
context["assignedto_list"] = [int(i) for i in request.POST.getlist('assigned_to', []) if i]
return render(request, template_name, context)
create_lead.html
<form id="lead_form" method="POST" action="" novalidate enctype="multipart/form-data">
<div class="overview_form_block row marl justify-content-center">
<div class="col-md-9">
<div class="card">
<div class="card-body">
<div class="card-title text-center">
{% if lead_obj %}EDIT{% else %}CREATE{% endif %} LEAD
</div>
<div class="row marl no-gutters">
<div class="col-md-4">
<div class="filter_col col-md-12">
<div class="form-group">
<div class="row">
<div class="filter_col col-md-6">
<label for="exampleInputEmail1" class="name" >First Name{% if lead_form.first_name.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.first_name }}
<span class="error error_message" id="error_id_first_name">{{ lead_form.first_name.errors }}</span>
</div>
<div class="filter_col col-md-6">
<label for="exampleInputEmail1" class="name" >Last Name{% if lead_form.last_name.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.last_name }}
<span class="error error_message" id="error_id_last_name">{{ lead_form.last_name.errors }}</span>
</div>
</div>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1" {% if request.POST.status == "converted" %} class="required" {% endif %}>Account Name{% if lead_form.account_name.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.account_name }}
<span class="error error_message" id="error_id_account_name">{{ lead_form.account_name.errors }}</span>
{% if error %}
<span class="error error_message" id="error_id_}">{{ error }}</span>
{% endif %}
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Title{% if lead_form.title.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.title }}
<span class="error error_message" id="error_id_title">{{ lead_form.title.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group ">
<label for="exampleInputEmail1">Phone{% if lead_form.phone.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.phone }}
<span class="error error_message" id="error_id_phone">{{ lead_form.phone.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group ">
<label for="exampleInputEmail1" class="required">Email Address{% if lead_form.email.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.email }}
<span class="error error_message" id="error_id_email">{{ lead_form.email.errors }}</span>
{% if error %}
<span class="error error_message" id="error_id_}">{{ error }}</span>
{% endif %}
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Attachment</label>
<input type="file" name="lead_attachment" id="lead_attachment">
{% if lead_obj.lead_attachment.count %}
{% for attachment in lead_obj.lead_attachment.all %}
<div id="attachment{{attachment.id}}" class="mt-2">
<a target="_blank" href="{{ attachment.attachment.url }}">{{ attachment.file_name }}</a>
{% if not attachment.created_by.role == 'ADMIN' or attachment.created_by.role == request.user.role %}
<a class="action btn primary_btn" onclick="remove_attachment({{attachment.id}})">X</a>
{% endif %}
</div>
{% endfor %}
{% endif %}
<span class="error"></span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Website{% if lead_form.website.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.website }}
<span class="error error_message" id="error_id_website">{{ lead_form.website.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1" >Description{% if lead_form.description.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.description }}
<span class="error error_message" id="error_id_description">{{ lead_form.description.errors }}</span>
</div>
</div>
{% if request.user.is_superuser or request.user.role == 'ADMIN' %}
<div class="filter_col col-12">
<div class="form-group">
<label for="id_sattus">Teams{% if lead_form.teams.field.required %}<span
class="error">*</span>{% endif %}</label>
{{ lead_form.teams }}
<span class="error" id="id__teams">{{ lead_form.teams.errors }}</span>
</div>
</div>
{% endif %}
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Assigned Users</label>
<select class="assigned_users form-control" name="assigned_to" multiple="multiple">
{% for user in users %}
<option value="{{user.id}}" {% if user in lead_obj.assigned_to.all or user.id in assignedto_list %} selected="" {% endif %}>{{user.email}}</option>
{% endfor %}
</select>
<span class="error error_message" id="error_id_assigned_to">{{ lead_form.assigned_to.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1"/>Status{% if lead_form.status.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.status }}
<span class="error error_message" id="error_id_status">{{ lead_form.status.errors }}</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1"/>Source{% if lead_form.source.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.source }}
<span class="error error_message" id="error_id_source">{{ lead_form.source.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1" >Address</label>
{{ lead_form.address_line }}
<span class="error error_message" id="error_id_address_line">{{ lead_form.address_line.errors }}</span>
</div>
{{ lead_form.street }}
<span class="error error_message" id="error_id_street">{{ lead_form.street.errors }}</span>
<div class="row" style="margin-top:10px;">
<div class="col-md-4">
{{ lead_form.city }}
<span class="error error_message" id="error_id_city">{{ lead_form.city.errors }}</span>
</div>
<div class="col-md-4">
{{ lead_form.state }}
<span class="error error_message" id="error_id_state">{{ lead_form.state.errors }}</span>
</div>
<div class="col-md-4">
{{ lead_form.postcode }}
<span class="error error_message" id="error_id_postcode">{{ lead_form.postcode.errors }}</span>
</div>
<div class="col-md-12" style="margin-top:10px;">
{{ lead_form.country }}
<span class="error error_message" id="error_id_country">{{ lead_form.country.errors }}</span>
</div>
</div>
</div>
<br>
<div class="filter_col col-12">
<div class="form-group">
<label>Tags</label>
<div class="txt-box-div" id="tag-div"><input type="text" name="tags" id="tags_1" value="{% for t in lead_obj.tags.all %}{{t.name}}, {% endfor %}" class="tags"/>
</div>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1"/>Type{% if lead_form.ctype.field.required %}<span class="error">*</span>{% endif %}</label>
{{ lead_form.ctype }}
<span class="error error_message" id="error_id_type">{{ lead_form.ctype.errors }}</span>
</div>
</div>
<div class="filter_col col-md-12">
<div class="form-group">
<label for="exampleInputEmail1">Products</label>
<select class="assigned_users form-control" name="products" multiple="multiple">
{% for pro in product %}
<option value="{{user.id}}" {% if pro in lead_obj.product.all %} selected="" {% endif %}>{{pro.name}}</option>
{% endfor %}
</select>
<span class="error error_message" id="error_id_products">{{ lead_form.product.errors }}</span>
</div>
</div>
</div>
<div class="col-md-12">
<div class="row marl buttons_row text-center form_btn_row">
<button class="btn btn-default save" type="submit" id="submit_btn">Save</button>
{% if not lead_obj %}
<button class="btn btn-success save savenew" type="submit">Save & New</button>
{% endif %}
Cancel
</div>
</div>
</div>
</div>
</div>`enter code here`
</div>
<input type="hidden" id="save_new_form" name="savenewform">
</div>
</form>
my ajax code
$('form#lead_form').ajaxForm({
type:'POST',
dataType:'json',
url: ".",
data:$('#lead_form').serialize(),
beforeSubmit: disableButton,
beforeSend: function() {
var percentVal = '0%';
bar.width(percentVal);
percent.html(percentVal);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal);
if (percentVal == '100%'){
percent.html('Uploading ...')
}
else{
percent.html(percentVal);
}
},
success: function(data) {
if(data.error){
$('#progress_bar').hide()
$('.error_message').html('')
for (var key in data.errors) {
$('#error_id_'+key).html("<p>" + data.errors[key][0] + "</p>");
};
$('#submit_btn').removeAttr('disabled')
}
else{
window.location = data.success_url;
}
}
});
The form should be submitted successfully but, it's not working and not giving me an error.
I have custom registration form with my own registration view. I've started to using django-allauth recently and I would like to add email confirmation to my registration. Is it possible?
registration view:
def registration(request):
user_creation_form = UserProfileCreationForm(request.POST or None)
if request.method == 'POST':
if user_creation_form.is_valid():
user_creation_form.save()
username = user_creation_form.cleaned_data['username']
password = user_creation_form.cleaned_data['password2']
user = authenticate(username=username, password=password)
login(request, user)
messages.add_message(request, messages.SUCCESS, YOU_HAVE_BEEN_REGISTERED)
return HttpResponseRedirect(reverse('homepage'))
return render(request, 'dolava/accounts/registration.html',
context={'user_creation_form': user_creation_form})
This is how rewrite allauth templates:
in SETTINGS.PY
TEMPLATE_LOADERS = (
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
)
TEMPLATE_DIRS = (os.path.join(BASE_DIR, 'templates','allauth'),)
main app templates:
templates/account/password_reset.html (overriden allauth templates)
The problem is that I want to have many fields in registration template like type_of_user, telephone etc.
registration.html:
{% extends 'base.html' %}
{% load static %}
{% block head %}
<script src="{% static "js/registrationToggleFields.js" %}"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#id_country').select2();
$('#id_telephone_0').select2();
});
</script>
{% endblock %}
{% block content %}
<div class="col-md-12 text-center">
<h2>Register your account</h2>
<hr class="col-md-12 blackhr no-bottom-margin">
</div>
{# <div class="col-md-12 text-center center-block">#}
<form action="" method="post" class="col-md-6 col-md-offset-3" id="register-form">
{% csrf_token %}
{{ user_creation_form.non_field_errors }}
<div id="form-account-type">
<h3 align="center" class="main-color">Account Type</h3>
<hr>
{# <div class="col-md-12">#}
<div id="type-of-user-field-wrapper" class="fieldWrapper">
<div>
<label for="{{ user_creation_form.type_of_user.id_for_label }}">Type of user:</label>
{% if user_creation_form.type_of_user.field.required %} * {% endif %}
</div>
{{ user_creation_form.type_of_user }}
{{ user_creation_form.type_of_user.errors }}
</div>
<div id="country-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.country.id_for_label }}">Country:</label>
{% if user_creation_form.country.field.required %} * {% endif %}
<br>
{{ user_creation_form.country }}
{{ user_creation_form.country.errors }}
</div>
{# </div>#}
<hr>
</div>
<div id="form-login-credentials" class="form-group-container show-allways">
<h3 align="center" class="main-color">Credentials</h3>
<hr>
<div>
<div id="username-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.username.id_for_label }}">Username:</label>
{% if user_creation_form.username.field.required %} * {% endif %}
<br>
{{ user_creation_form.username }}
{{ user_creation_form.username.errors }}
</div>
<div id="password1-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.password1.id_for_label }}">Password:</label>
{% if user_creation_form.password1.field.required %} * {% endif %}
<br>
{{ user_creation_form.password1 }}
{{ user_creation_form.password1.errors }}
</div>
<div id="password2-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.password2.id_for_label }}">Confirm your password:</label>
{% if user_creation_form.password2.field.required %} * {% endif %}
<br>
{{ user_creation_form.password2 }}
{{ user_creation_form.password2.errors }}
</div>
</div>
<hr>
</div>
<div id="form-personal-information" class="form-group-container show-personal">
<h3 align="center" class="main-color">Personal information</h3>
<hr>
<div>
<div id="first-name-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.first_name.id_for_label }}">First name:</label>
{% if user_creation_form.first_name.field.required %} * {% endif %}
<br>
{{ user_creation_form.first_name }}
{{ user_creation_form.first_name.errors }}
</div>
<div id="last-name-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.last_name.id_for_label }}">Last name:</label>
{% if user_creation_form.last_name.field.required %} * {% endif %}
<br>
{{ user_creation_form.last_name }}
{{ user_creation_form.last_name.errors }}
</div>
</div>
<hr>
</div>
<div id="form-company-information" class="form-group-container show-company-sk show-company">
<h3 align="center" class="main-color">Company information</h3>
<hr>
<div>
<div id="company-name-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.company_name.id_for_label }}">Company name:</label>
{% if user_creation_form.company_name.field.required %} * {% endif %}
<br>
{{ user_creation_form.company_name }}
{{ user_creation_form.company_name.errors }}
</div>
<div id="address-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.address.id_for_label }}">Address:</label>
{% if user_creation_form.address.field.required %} * {% endif %}
<br>
{{ user_creation_form.address }}
{{ user_creation_form.address.errors }}
</div>
</div>
<hr>
</div>
<div id="form-billing-information" class="form-group-container show-company-sk">
<h3 align="center" class="main-color">Billing information</h3>
<hr>
<div>
<div id="ico-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.ICO.id_for_label }}">IČO:</label>
{% if user_creation_form.ICO.field.required %} * {% endif %}
<br>
{{ user_creation_form.ICO }}
{{ user_creation_form.ICO.errors }}
</div>
<div id="dic-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.DIC.id_for_label }}">DIČ:</label>
{% if user_creation_form.DIC.field.required %} * {% endif %}
<br>
{{ user_creation_form.DIC }}
{{ user_creation_form.DIC.errors }}
</div>
</div>
<hr>
</div>
<div id="form-contact-information" class="form-group-container show-allways">
<h3 align="center" class="main-color">Contact information</h3>
<hr>
<div>
<div id="email-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.email.id_for_label }}">Email:</label>
{% if user_creation_form.email.field.required %} * {% endif %}
<br>
{{ user_creation_form.email }}
{{ user_creation_form.email.errors }}
</div>
<div id="telephone-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.telephone.id_for_label }}">Telephone:</label>
{% if user_creation_form.telephone.field.required %} * {% endif %}
<br>
{{ user_creation_form.telephone }}
{{ user_creation_form.telephone.errors }}
</div>
<div id="fax-field-wrapper" class="fieldWrapper">
<label for="{{ user_creation_form.fax.id_for_label }}">Fax:</label>
{% if user_creation_form.fax.field.required %} * {% endif %}
<br>
{{ user_creation_form.fax }}
{{ user_creation_form.fax.errors }}
</div>
</div>
</div>
<div class=" clearfix top-margin">
<button class="btn btn-success center-block" style="width:100%; padding: 15px 0;" type="submit">
Register
</button>
</div>
</form>
{# </div>#}
{% endblock %}
Do you have any advices?
You have to override one class and then supply all required variables to thhe specific template. The view that returns confirmation_email is ConfirmEmailView of allauth/account/views.py override this class, pass all the required variables from view to the template and then override your template account/email_confirm to show required data.
When I access the update page it doesn't populate the fields with the existing entry data (which is there, and print statements I've placed in parts of the view show that it's accessible and exists), I'm not really sure why it's not populating.
This is my view:
#login_required
def sites_update_view(request, place_id=None):
if place_id:
place = get_object_or_404(SiteMeta, pk=place_id)
else:
return redirect('sites-index')
if request.POST:
form = SitesAddForm(request.POST, instance=place)
if form.is_valid():
form.save()
return redirect('sites-index')
else:
form = SitesAddForm(instance=place)
return render(request, 'sites-update.html', {
'form': form,
'site': place,
'place_id': place_id
})
My template:
{% extends "newbase.html" %}
{% load url from future %}
{% load floppyforms %}
{% load staticfiles %}
{% block title %} - Update Site {% endblock %}
{% block content %}
<div class="row">
<div class="col-sm-6 col-md-6">
<h3 class="heading">Update Surveillance Site</h3>
<form method="post" action={% url 'sites-update' place_id=site.pk %}>
{% csrf_token %}
<div class="formSep">
<div class="row">
<div class="col-sm6 col-md-6">
<label for="id_name">Site Name:<span class="f_req">*</span></label>
{{ form.name }}
<span class="help-block">What is the site name?</span>
</div>
<div class="col-sm-6 col-md-6">
<label for="id_lga">LGA:<span class="f_req">*</span></label>
{{ form.lga }}
<span class="help-block">What is the LGA?</span>
</div>
<div class="col-sm-6 col-md-6">
<label for="id_site_type">Site Type:<span class="f_req">*</span></label>
{{ form.site_type }}
<span class="help-block">What type of site is this?</span>
</div>
<div class="col-sm-6 col-md-6">
<label for="id_site_priority">Site Priority:<span class="f_req">*</span></label>
{{ form.site_priority }}
<span class="help-block">What is the priority of this site?</span>
</div>
<div class="col-sm-6 col-md-6">
<label for="id_site_category">Site Category:<span class="f_req">*</span></label>
{{ form.site_category }}
<span class="help-block">What category should the site be in?</span>
</div>
</div>
</div>
<div class="row">
<input class="btn btn-default" type="submit" value="Save" />
<a class="btn btn-default" href={% url "sites-index" %}>Cancel</a>
</div>
</form>
</div>
</div>
{{ form.errors }}
{% endblock %}
{% block sidebar %}
{% include "afp-sidebar.html" %}
{% endblock %}
if request.POST: should be if request.method == 'POST':