I am using Django to create an app. I have a list of objects and a user can select one and go to a page with the details of that object on it, they can then change these details. However, at the moment, the form which is used to change the details asks the user to enter the name of the object before changing the details. I would like it to have a default value already entered, this default value should be the one the user selected and viewed on the previous page i.e. the user selects 'object1', they go to the form to change the information for 'object1' and at the top of that form there is a label which says 'Object name' and the input box has 'object1' in it without the user doing anything to it. Here is some of my code, I hope this all makes sense. Has anyone got any suggestions on how I could do this? I'm thinking I could use RESTful API URLs, so the url could be something like /details/2/change_date
then maybe it could access the name for id 2?
Urls.py
urlpatterns = [
url(r'^details/$', Info.as_view(), name="server_details"),
url(r'^details/([0-9]+)/$', GetDetails.as_view(), name="details"),
url(r'^change_date/$', ChangeDateFormView.as_view(), name="change_date"),
]
Views.py
class ChangeDateFormView(FormView):
def get_form_class(self):
return ChangeDateForm
def get_template_names(self):
return 'change_date.html'
# Renders a form on GET and processes it on POST.
def get(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
return self.render_to_response(self.get_context_data(form=form))
def post(self, request, *args, **kwargs):
form_class = self.get_form_class()
form = self.get_form(form_class)
if form.is_valid():
form_data = form.cleaned_data
name = form.data['name']
new_date = form_data['new_date']
confirm_new_date = form_data['confirm_new_date']
if new_date != confirm_new_date:
form._errors["new_date"] = "Dates do not match"
return self.form_invalid(form)
elif MyObjects.objects.filter(name=name).exists():
MyObjects.objects.filter(name=name).update(date=new_date)
return render(request, 'change_data_success.html', {})
else:
form.errors['name'] = "doesn't exists"
return self.form_invalid(form)
else:
return self.form_invalid(form)
Template
<form action="{% url "change_date" %}" method="post">
{% csrf_token %}
<div class="form-group cf">
<label for="name" class="col-sm-4 control-label">name:</label>
<div class="col-sm-8">
{% render_field form.name type="name" class+="form-control" placeholder="name" %}
<span style="color: #FF0000">{{ form.name.errors }}</span>
</div>
</div>
<div class="form-group cf">
<label for="date" class="col-sm-4 control-label">New date:</label>
<div class="col-sm-8">
{% render_field form.new_date type="date" class+="form-control" %}
<span style="color: #FF0000">{{ form.new_date.errors }}</span>
</div>
</div>
<div class="form-group cf">
<label for="confirm date" class="col-sm-4 control-label">Confirm date:</label>
<div class="col-sm-8">
{% render_field form.confirm_new_date type="date" class+="form-control" %}
<span style="color: #FF0000">{{ form.confirm_new_date.errors }}</span>
</div>
</div>
<div style="text-align: center">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</form>