توی یه اپلیکیشن یه فایل HTML داریم که سه تا فرم توشه ولی فقط با یه فرم داره رندر میشه. مشکل اینجاست که اینجوری پر از باگه. حتی ممکنه فرم اول رو سابمیت کنی کد فرم بعدی هم اجرا شه در حالی که اصلا لازم نیست هم از نظر performance ای یه کد اضافی داره اجرا میشه هم ممکنه دیتای فرم بعدی رو خراب کنه. برای اینکار به سه تا input فرم ها هر کدوم یه اسم یکتا دادم مثلا:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
form1 . . . <input type="submit" class="btn btn-lg btn-success" name="document_form_submit" value="تایید"> . . . form2 . . . <input type="submit" class="btn btn-lg btn-success" name="feedback_form_submit" value="ثبت"> . . . form3 . . . <button type="submit" class="btn btn-primary" name="comment_form_submit">ثبت</button> |
حالا توی فرم چک میکنیم که این فیلد توی request هست یا نه:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
def documents_document_detail(request, doc_id): d = Document.objects.get(pk=doc_id) feedback_form = FeedbackForm() context = { 'feedback_comment_form': FeedbackCommentForm(), 'doc': d, 'id': doc_id, 'service': d.system, 'form': DocumentForm(model_to_dict(d), None), 'doc_attachments': d.attachments.filter(doc_content=0).all(), 'report_attachments': d.attachments.filter(doc_content=1).all(), 'comment_attachments': d.attachments.filter(doc_content=2).all(), 'feedback_form': feedback_form, 'comment_list': DocumentComment.objects.filter(document=d, user=request.user) } if hasattr(d, 'feedback'): feedback_form = FeedbackForm(initial={ 'platform_awareness': d.feedback.platform_awareness, 'main_points': d.feedback.main_points, 'secondary_points': d.feedback.secondary_points, 'guids_and_recommendations': d.feedback.guids_and_recommendations }) context['feedback_form'] = feedback_form if request.POST and 'document_form_submit' in request.POST: document_form = DocumentForm(request.POST, initial={'system': d.system}) document_form.fields.pop('system') document_form.fields.pop('templateversion') document_form.fields.pop('doc_type') document_form.fields.pop('docdate') if document_form.is_valid(): status = document_form.cleaned_data['status'] d.status = status d.save() context['doc'] = d context['form'] = DocumentForm(model_to_dict(d), None) context['doc_attachments'] = d.attachments.filter(doc_content=0).all() context['report_attachments'] = d.attachments.filter(doc_content=1).all() context['comment_attachments'] = d.attachments.filter(doc_content=2).all() messages.success(request, '...') return render(request, "man/documents/document-detail.html", context) else: for _, error in document_form.errors.items(): messages.error(request, error) return render(request, "man/documents/document-detail.html", context) if request.POST and 'feedback_form_submit' in request.POST: feedback_form = FeedbackForm(request.POST) if feedback_form.is_valid(): platform_awareness = feedback_form.cleaned_data['platform_awareness'] main_points = feedback_form.cleaned_data['main_points'] secondary_points = feedback_form.cleaned_data['secondary_points'] guids_and_recommendations = feedback_form.cleaned_data['guids_and_recommendations'] if hasattr(d, 'feedback'): d.feedback.platform_awareness = platform_awareness d.feedback.main_points = main_points d.feedback.secondary_points = secondary_points d.feedback.guids_and_recommendations = guids_and_recommendations d.feedback.save() feedback_form = FeedbackForm(initial={ 'platform_awareness': platform_awareness, 'main_points': main_points, 'secondary_points': secondary_points, 'guids_and_recommendations': guids_and_recommendations }) else: f = Feedback.objects.create( document=d, platform_awareness=platform_awareness, main_points=main_points, secondary_points=secondary_points, guids_and_recommendations=guids_and_recommendations ) feedback_form = FeedbackForm(initial={ 'platform_awareness': f.platform_awareness, 'main_points': f.main_points, 'secondary_points': f.secondary_points, 'guids_and_recommendations': f.guids_and_recommendations }) context['feedback_form'] = feedback_form messages.success(request, '...د') return render(request, "man/documents/document-detail.html", context) else: for _, error in feedback_form.errors.items(): messages.error(request, error) return render(request, "man/documents/document-detail.html", context) if request.POST and 'comment_form_submit' in request.POST: feedback_comment_form = FeedbackCommentForm(request.POST) if feedback_comment_form.is_valid(): body = feedback_comment_form.cleaned_data['body'] DocumentComment.objects.create( document=d, user=request.user, body=body, comment_datetime=datetime.datetime.now() ) context['comment_list'] = DocumentComment.objects.filter(document=d, user=request.user) messages.success(request, '...') return render(request, "man/documents/document-detail.html", context) else: for _, error in feedback_comment_form.errors.items(): messages.error(request, error) return render(request, "man/documents/document-detail.html", context) return render(request, "man/documents/document-detail.html", context) |
این جوری اگر درخواست POST باشه فقط اون فرمی که سابمیت شده کدش اجرا میشه و لازم هم نیست که برای هر فرم یک view جدا هم بنویسیم.