0

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!

  • Are you sure that is the exact template you are using? The console clearly shows you are sending the data as a GET, but your template has `method="post"`. – Daniel Roseman Dec 16 '18 at 13:39
  • Hi, Yes it is the exact template I'm using. I'm not sure why its posting a get request in the form even though its specified as POST. In django admin it fulfills a post request. – newdjangouser Dec 17 '18 at 10:20

3 Answers3

0

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

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

I had a problem like this too. I solved it by getting rid of a div tag containing a bootstrap class (<div class="form-group>" to be more precise) located around my form.

Takamura
  • 347
  • 5
  • 12
0

same problem existed for me also , the silly mistake you have done is the button thing in your create.html file , replace that with input tap with type submit and class btn , button doesn't submit request Try this :

<input type="submit" class="btn btn-primary" value="Submit">

i know its bit late but this may seem helpful