Django表单未提交或未提供错误消息 [英] Django form not submitting or providing error messages

查看:155
本文介绍了Django表单未提交或未提供错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

When I submit my form, it doesn't post the form data and just reloads the form. It was working beforehand but I'm not sure what I've changed that doesn't make it work anymore. Posting the data through the admin still works fine.

我只能在终端中看到错误消息:
可以在此处看到

The only 'error' message I can see is in the terminal: which can be seen here

它发送一个get请求而不是发布请求。我还测试了删除JS和引导CDN的方法,但是问题仍然存在。

It sends a get request instead of a post request as well. I've also tested it with removing the JS and bootstrap CDNs but the issue is still the same.

我的代码如下:

这是我的views.py

Here is my views.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

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

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

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})

预先感谢大家,
会非常感谢所有帮助!

Thanks everyone in advance, All help will be greatly appreciated!

推荐答案

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

You may have deleted your action. Try adding the url back?

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

这篇关于Django表单未提交或未提供错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆