I am looking for some assistance in two areas for Django form processing.
I have a class view that overrides post() and checks the name of the form in request.POST to determine which form has been submitted. I then perform the appropriate actions for whichever form was submitted and save to the model. That part works correctly. I am not using a model form, just a custom HTML form created in the template with <input> fields. See the below view and HTML for reference. Is this the correct way to handle this or is there a best practice I should be following that makes use of model forms? Code seems a bit heavy to me and non-standardized, like there should be a better way...
As I am not using model forms, the error processing has me a little confused. How do you handle error processing on a normal HTML form that does not make use of Django model forms? See below in the code for the view where it says # ERROR HANDLING FOR FORM NEEDED, specifically on the username field, which is unique and validated on the model level.
views.py:
class ProfileView(View):
def get(self, request, *args, **kwargs):
if request.method == "GET":
# load profile data...
def post(self, request, *args, **kwargs):
if request.method == "POST":
# check if profile_form submitted
if 'profile_form' in request.POST:
# get user form data
profile_data = request.POST.dict()
# get current user profile
user_profile = Profile.objects.get(my_user=request.user)
# check username entry against current
if user_profile.username == profile_data['username']:
user_profile.country = profile_data['country']
user_profile.display_location = profile_data['display_location']
user_profile.organization_name = profile_data['organization']
user_profile.save(update_fields=['country', 'display_location','organization_name'])
#send success message to page
messages.success(request, "Success: Profile was updated.")
else:
try:
user_profile.username = profile_data['username']
user_profile.country = profile_data['country']
user_profile.display_location = profile_data['display_location']
user_profile.organization_name = profile_data['organization']
user_profile.save(update_fields=['username', 'country', 'display_location','organization_name'])
#send success message to page
messages.success(request, "Success: Profile was updated.")
except:
# unique constraint error on username
# ERROR HANDLING FOR FORM NEEDED
# check if user_name_form submitted
if 'user_name_form' in request.POST:
# get user form data
user_data = request.POST.dict()
# get current user
user = MyUser.objects.get(email=request.user)
user.first_name = user_data['first_name']
user.last_name = user_data['last_name']
user.save(update_fields=['first_name', 'last_name'])
# send success message to page
messages.success(request, "Success: Name was updated.")
# Return Profile page view with updated data
return HttpResponseRedirect(reverse('profile'))
Template:
<!--form-->
<form id="profile" class="small" method="POST" action="{% url 'profile' %}">
{% csrf_token %}
<!--Username-->
<label for="username">Username <span style="font-style: italic;">(create a unique display name that will appear to other users on the site)</span></label>
<div class="input-group mb-3">
<div class="input-group-prepend">
<span class="input-group-text" id="username">@</span>
</div>
<input type="text" class="form-control" placeholder="Username" aria-label="Username" aria-describedby="username" name="username" value="{% if profile and profile.username is not None %}{{ profile.username }}{% endif %}">
</div>
<hr>
<p>Tell us where you are from!</p>
<!--Country State/Province City Select-->
<div class="form-group">
<select name="country" class="form-control mb-3" id="country">
<option value="">Select Country...</option>
{% for country in countries %}
{% if profile.country == country.name %}
<option value="{{ country.name }}" id="{{ country.code }}" selected>{{ country.name }}</option>
{% else %}
<option value="{{ country.name }}" id="{{ country.code }}">{{ country.name }}</option>
{% endif %}
{% endfor %}
</select>
</div>
<hr>
<p>Enter your profile display location <i>(ie. City, State, Province, Region...)</i></p>
<input type="text" class="form-control" id="display_location" name="display_location" placeholder="Based in..." value="{% if profile and profile.display_location is not None %}{{ profile.display_location }}{% endif %}">
<hr>
<p>Do you belong to an organization?</p>
<input type="text" class="form-control" id="organization" name="organization" placeholder="Organization Name" value="{% if profile and profile.organization_name is not None %}{{ profile.organization_name }}{% endif %}">
<hr>
<button type="submit" class="btn btn-primary" name="profile_form" value="profile_form">Save</button>
</form>
As a solution to this problem I was able to get to, see the post here for a way to do this without a 3rd party app.