Django form not submitting or providing error messages

点点圈 提交于 2019-12-02 15:42:44

问题


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

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.

My code is below:

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

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

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


回答1:


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

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


来源:https://stackoverflow.com/questions/53802015/django-form-not-submitting-or-providing-error-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!