Created a DJango app to track techs daily job, posting data from other website via JavaScript DOM (sending the entire webpage to server) where using xpath to look for element and extract their values.
What I would like is o be able to edit/validate by the user the data before saving it.
For example add job-in and job-out times what’s not available in the original |HTML page.
Most of my work created from examples, can’t really find example to how to integrate this edit function.
#in veews.py
def post_work_order(request):
if request.method == "POST":
try:
data = request.POST.get('DOM')
tree = html.fromstring(data)
name = tree.xpath(
'//*[@id="userNameWrapper"]/span')[0].text.strip().split("\\")[1]
....(rest is similar extraction)
job.save()
args = {}
args["job"] = job.id
return render(request, 'dashboard/edit_job.html', args)
#in fomrs.py
from django import forms
from .models import Job
class JobEditForm(forms.ModelForm):
class Meta:
model = Job
fields = ['work_order', 'address', 'customer_name', 'codes', 'note']
exclude = ('cerated_at', 'updated_at')
#dont know how to proceed from here !!
#in edit_job.html
{% load static %}
{% block content %}
{% load crispy_forms_tags %}
<form method="post">
{% csrf_token %}
{% crispy form %}
{% for formset in inlines %}
{{ formset|crispy }}
{% endfor %}
<input type="submit" value="Submit" />
</form>
{% endblock%}
Start with Using forms to validate data to see what functionality Django provides. (It might also be helpful to review the page Working with forms.)
There isn’t any form to begin with, not sure how to pass the extracted data to {% crispy form %}
#error in edit_job.html
Exception has occurred: VariableDoesNotExist
Failed lookup for key [form] in [{'True': True, 'False': False, 'None': None}, {'csrf_token': <SimpleLazyObject: '8ddUj9BnXHrrzFAwrzDAFv9aaOxeVDr5kR6euFoMDN71mUIjhILoBr1ko3Kon36V'>, 'debug': True, 'sql_queries': <function debug.<locals>.<lambda> at 0x000001A6380A1DC0>, 'request': <WSGIRequest: POST '/tech/post-work-order/?uri=%20https://wesite_hiden_from_forum'>, 'user': <SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x000001A6388300D0>>, 'perms': <django.contrib.auth.context_processors.PermWrapper object at 0x000001A6380708E0>, 'messages': <django.contrib.messages.storage.fallback.FallbackStorage object at 0x000001A63712DF40>, 'DEFAULT_MESSAGE_LEVELS': {'DEBUG': 10, 'INFO': 20, 'SUCCESS': 25, 'WARNING': 30, 'ERROR': 40}}, {}, {'job': 863}, {'block': <Block Node: content. Contents: [<TextNode: '\n'>, <django.template.defaulttags.LoadNode object at 0x000001A63809F430>, <TextNode: '\n<form method="post">\n '>, <django.template.defaulttags.CsrfTokenNode object at 0x000001A63809F490>, <TextNode: '\n '>, <crispy_forms.templatetags.crispy_forms_tags.CrispyFormNode object at 0x000001A63809F520>, <TextNode: '\n\n '>, <ForNode: for formset in inlines, tail_len: 3>, <TextNode: '\n\n <input type="submit" '>]>}]
#error in views.py
Exception has occurred: VariableDoesNotExist
Failed lookup for key [form] in [{'True': True, 'False': False, 'None': None}, {}, {}, {'job': 863}]
Ok, I think I’m following you now. The JobEditForm you’ve posted really isn’t part of this process.
If you’re just receiving an html page as the post data, my initial thought would be to just extract and validate the individual components from within the view. I wouldn’t try to wedge this extracted data into a form - I’m not sure it buys you any benefits to do that.
My initial reaction would be to create one or more validation classes to handle the data. But I can’t think of anything intrinsic to Django to facilitate this process.