简体   繁体   English

Django 表格未显示在 html 模板中

[英]Django form are not showing in html template

I'm trying to create a form in Django template but it is just not showing the fields我正在尝试在 Django 模板中创建一个表单,但它只是不显示字段

here is my files这是我的文件

models.py where i created the desired table models.py我在其中创建了所需的表

class ReportMessage(models.Model):
    sender = models.ForeignKey(UserModel, related_name="report_message_sender", on_delete='CASCADE')
    message = models.ForeignKey(Message, on_delete='CASCADE')
    created_at = models.DateTimeField(auto_now=True)
    reason = models.TextField(max_length=1500)
    is_read = models.BooleanField(default=False)

forms.py where i created the form to edit only one field in the table forms.py我在其中创建表单以仅编辑表中的一个字段

class ReportMessageForm(forms.Form):
    class Meta:
        model = ReportMessage
        fields = ['reason', ]

views.py where i created the view for the form views.py我在其中创建了表单的视图

@login_required
def report_message(request, pk):
    current_user = request.user
    reported_message = get_object_or_404(Message, pk=pk)
    if request.method == "POST":
        report_message_form = ReportMessageForm(request.POST)
        if report_message_form.is_valid():
            model_instance = report_message_form.save(commit=False)
            model_instance.sender = current_user
            model_instance.message = reported_message
            model_instance.save()
            return redirect('report_confirm')
    else:
        report_message_form = ReportMessageForm()

    context = {
        'report_message_form': report_message_form,
    }
    return render(request, 'fostania_web_app/report_message.html', context)


def report_confirm(request):
    return render(request, 'fostania_web_app/report_confirm.html')

and urls.py where the urls i used for the viewsurls.py我用于视图的 url

path('report/messages/<int:pk>/', views.report_message, name="report_message"),
path('report/confirm', views.report_confirm, name="report_confirm"),

and finally that is how i used the form in the html template最后这就是我在html模板中使用表单的方式

{% extends 'fostania_web_app/base.html' %}
{% block content %}
{% load static %}
     <form action="" method="post" name="ReportMessageForm" align="right">
         {% csrf_token %}
{{ report_message_form }}
         <input type="submit" class="btn btn-success" style="width: 100px;" value="إرسال" />
     </form>
{% endblock %}

and then all what i see in the html page is the submit button and there is no form labels or input or anything.然后我在 html 页面中看到的只是提交按钮,没有表单标签或输入或任何内容。

I think that your problem is in your model form because you are using forms.Form and you need to use forms.ModelForm 我认为您的问题出在模型表单中,因为您使用的是Forms.Form,并且需要使用Forms.ModelForm

class ReportMessageForm(forms.ModelForm):
   class Meta:
      model = ReportMessage
      fields = ['reason', ]

In your forms.py if you are not using ModelForm then you have to explicitly declare the fields for the forms 如果您不使用ModelForm,则在forms.py中必须显式声明表单的字段

reason = forms.Charfield()

Or you can use ModelForm which inherits from the model you specify. 或者,您可以使用从您指定的模型继承的ModelForm。

You should specify the model in the Meta class while using ModelForm.You can also specify required fields from the Model in the fields list in Meta class 使用ModelForm时应在Meta类中指定模型。还可以从Meta类的字段列表中的Model中指定必填字段

Class myform(forms.ModelForm)
   Class Meta:
      model = your_model_name
      fields= [reason,]

Cheers :) 干杯:)

def report_confirm(request):
    return render(request, 'fostania_web_app/report_confirm.html', context) #add the context 

You need to pass in the "context" so that it shows in the template您需要传入“上下文”,以便它显示在模板中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM