Django表单未提交或未提供错误消息

时间:2018-12-16 12:17:25

标签: django django-forms python-requests django-csrf csrf-token

提交表单时,它不会发布表单数据,而只是重新加载表单。 它事先已经在工作,但是我不确定我所做的更改不能使它工作了。通过管理员发布数据仍然可以正常工作。

我只能在终端中看到“错误”消息: which can be seen here

它也发送get请求而不是post请求。我还测试了删除JS和引导CDN的方法,但是问题仍然相同。

我的代码如下:

这是我的观点。py

def create(request):
if request.method == 'POST':
    form = EventCreateForm(request.POST, request.FILES)
    if form.is_valid():.
        instance = form.save(commit=False)
        instance.author = request.user
        instance.save()
        instance.save_m2m()
        return redirect('event:home')
else:
    form = EventCreateForm()
return render(request, 'event/create.html', {'form': form})

create.html

{% extends "base.html" %}

{% load crispy_forms_tags %}


{% block content %}

{{ form.media }}
<div class="container-fluid">
    <div class="col-md-4">
        <div class="page-header">
            <p> Create an event!</p>
        </div>
        <form  method="post" action="" enctype="multipart/form-data">
                {% csrf_token %}
            {{ form | crispy}}

            <button type="submit">Submit</button>
            <br>
            <br>
        </form>
    </div>
</div>



{% endblock %}

Base.html

<!DOCTYPE html>

<html lang="en">
<head>
    <meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0">





<script src="https://code.jquery.com/jquery-3.2.1.min.js" crossorigin="anonymous" integrity="sha384-xBuQ/xzmlsLoJpyjoggmTEz8OWUFM0/RC5BsqQBDX2v5cMvDHcMakNTNrHIW2I5f"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" crossorigin="anonymous" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"></script>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" crossorigin="anonymous" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"></script>


<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">




{% load static %}
<link rel="stylesheet" type="text/css" href="{% static 'event/css/custom.css' %}">
        <title>Django Project</title>
    </br>
    <div class="container-fluid" style='font-family:arial'>
        <center>
            <h2> Welcome to the django website!</h2>
        </center>
    </div>
    {{ form.media }}
</head>



<!-- body of the text -->
    <body>

        {% if messages %}
            {% for messages in messages %}
                {{ message }}
            {% endfor %}
        {% endif %}

        {% if user.is_authenticated %}
    <nav class="navbar navbar-expand-md navbar-dark bg-dark sticky-top">
        <div class="navbar-nav">
            <a class="nav item nav-link active" href="{% url 'event:home' %}">Home</a>
            <a class="nav item nav-link" href="{% url 'profiles:profile' %}">Profile</a>
            <a class="nav item nav-link" href="{% url 'profiles:edit' %}">Edit profile</a>
            <a class="nav item nav-link" href="{% url 'event:create' %}">Create</a>
            <a class="nav item nav-link" href="{% url 'profiles:logout' %}">Logout</a>
            <form class="form-inline my-2 my-lg-0">
                <input class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
                <button class="btn btn-success" type="submit">Search</button>
        </div>

        {% else %}
            <a href="{% url 'profiles:login' %}">Login</a>
            <a href="{% url 'profiles:register' %}">Register</a>
        {% endif %}
    </nav>
        {% block content %}




        {% endblock %}




    </body>
</html>

Models.py

class Event(models.Model):
    title = models.CharField(max_length=100)
    link = models.TextField()
    author = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
    image = models.ImageField(default='default.jpg', upload_to='event_images')
    image_thumbnail = ImageSpecField(source='image',
                                        processors=[ResizeToFill(100, 100)],
                                        format='JPEG',
                                        options={'quality': 60})
    start = models.DateField(blank=True, null=True)
    start_time = models.TimeField(blank=True, null=True)
    end = models.DateField(blank=True, null=True)
    end_time = models.TimeField(blank=True, null= True)
    description = HTMLField('description')
    tags = models.ManyToManyField(Tags)
    subject = models.ManyToManyField(Subject)
    attendees = models.ManyToManyField(User, related_name = 'attendees', blank=True)

    def __str__(self):
        return f'{self.title}'

    def get_absolute_url(self):
        return reverse('event:detail', kwargs={'pk': self.pk})

预先感谢大家, 所有帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您可能已删除了操作。尝试重新添加网址?

  <form  method="post" action="{% url "event:create" %}" enctype="multipart/form-data">

答案 1 :(得分:0)

我也遇到过这样的问题。我通过去掉一个包含引导类(更准确地说是 <div class="form-group>")的 div 标签解决了这个问题。