Home > Software design >  Why is my create button not redirecting to the form? (For django)
Why is my create button not redirecting to the form? (For django)

Time:01-20

I am new to Django and am trying to make an online journal, and the server runs fine, but when I press the create button, it does not redirect to the form even though a POST request is being sent. screenshot of the create page. I'm not sure why.

This is the code:

views.py:

from django.shortcuts import render, redirect, get_object_or_404
from ejournal.models import Journal
from ejournal.forms import JournalForm


def make_entry(request):
    if request.method == "POST":
        entry = JournalForm(request.POST, request.FILES)
        if entry.is_valid():
            entry.save()
            return redirect('list_journals')
    else:
        entry = JournalForm()
    context = {
        'entry':entry 
    }
    return render(request, 'ejournal/create.html', context)

def delete_entry(request, id):
    entry = get_object_or_404(Journal, id=id)
    if request.method == "POST":
        entry.delete()
        return redirect('list_journals')
    context = {
        'object':object

    }
    return render(request, 'journal/delete.html',context)

def list_journals(request):
    entries = Journal.objects.all()
    context = {
        'entries': entries
    }
    return render(request, 'ejournal/list_journals.html',context)

def journal_detail(request, id):
    journal = Journal.objects.get(id=id)
    context = {
        'journal':journal
    }
    return render(request, 'journal/journal_detail.html', context)

forms.py

from ejournal.models import Journal 
from django.forms import ModelForm

class JournalForm(ModelForm):
    class Meta:
        model = Journal 
        fields = ['name','title','text','image']

models.py

from django.db import models


class Journal(models.Model):
    name = models.CharField(max_length=20)
    title = models.CharField(max_length=50)
    text = models.TextField(max_length=1000)
    date = models.DateField(auto_now_add=True)
    image = models.FileField(upload_to='')
    archived = models.BooleanField(default=False)
    
    def __str__(self):
        return self.name


base.html

{% load static %}<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>My Online Journal</title>
    <!-- Bootstrap core CSS -->
    <link href="{% static 'css/bootstrap.min.css' %}" rel="stylesheet">
    <link href="{% static 'css/style.css' %}" rel="stylesheet">
  </head>
  <body>

    <!-- Navigation -->
    <nav >
      <div >
        <a  href="{% url 'list_journals' %}">Smart Journal</a>
        <button  type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation">
          <span ></span>
        </button>
        <div  id="navbarResponsive">
          <ul >
            <li >
              <a  href="{% url 'list_journals' %}">LIST ALL</a>
            </li>
          </ul>
        </div>
        <div  id="navbarResponsive">
          <ul >
            <li >
              <a  href="{% url 'make_entry' %}">  New Journal Entry </a>
            </li>
          </ul>
        </div>
      </div>
    </nav>

    <!-- Page Content -->
    <div  id="app">
    
        {% block content %}
        {% endblock %}
    </div>
    <!-- /.container -->

    <!-- Bootstrap core JavaScript -->
    <script src="{% static 'js/jquery.min.js' %}"></script>
    <script src="{% static 'js/bootstrap.min.js' %}"></script>
  </body>
</html>

create.html

{% extends 'ejournal/base.html' %}
{% block content %}
    <div >
        <h1> Create Journal Entry </h1>
    </div>
    <div >
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <p>{{ form.as_p }}</p>
            <button  type="submit">CREATE</button>
        </form>
    </div>
    
{% endblock content %}

urls.py

from django.urls import path 
from ejournal.views import make_entry, delete_entry, list_journals, journal_detail

urlpatterns = [
    path('create', make_entry, name="make_entry"),
    path('list_journals', list_journals, name="list_journals"),
    path('delete/<int:id>/', delete_entry, name="delete_entry"),
    path('journal_detail/<int:id>',journal_detail, name="journal_detail"),
]

CodePudding user response:

Check with form's action='' attribute like this...

<form method="POST" action="{% url 'make_entry' %}" enctype="multipart/form-data">
  {% csrf_token %}
  <p>{{ form.as_p }}</p>
  <button  type="submit">CREATE</button>
</form>

CodePudding user response:

You pass the form as the variable entry to your template ejournal/create.html. Inside this template you use {{ form.as_p }}, but form is undefined. You need to use the variable entry because thats where the form is assigned to.

So your create.html becomes this:

{% extends 'ejournal/base.html' %}
{% block content %}
    <div >
        <h1> Create Journal Entry </h1>
    </div>
    <div >
        <form method="POST" enctype="multipart/form-data">
            {% csrf_token %}
            <p>{{ entry.as_p }}</p>
            <button  type="submit">CREATE</button>
        </form>
    </div>
    
{% endblock content %}

You're checking if your form is valid by using entry.is_valid(), but your form will always be invalid because you didn't pass the correct form in your template, as described above.

  • Related