I am creating a form for users to be able to create listings for an auction page however when I click the link from my index page to take me to the create page, i get redirected to the create page but on the browser, the form does not show.
INDEX.HTML
<p>You can create your first auction here <a href={% url 'create_listing' %}>Add new</a></p>
URLS.PY
path("create/", views.create_listing, name="create_listing")
MODELS.PY
from django.contrib.auth.models import AbstractUser
from django.db import models
class User(AbstractUser):
pass
class Auction(models.Model):
title = models.CharField(max_length=25)
description = models.TextField()
current_bid = models.IntegerField(null=False, blank=False)
users_bid = models.IntegerField(null=False, blank=False)
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
VIEWS.PY
from django.shortcuts import render, redirect
from django.template import context
from .forms import AuctionForm
from .models import User
def create_listing(request):
form = AuctionForm()
if request.method == 'POST':
form = AuctionForm(request.POST)
if form.is_valid:
form.save()
return redirect('index')
context = {'form': form}
return render(request, 'auctions/create-listing.html', context)
FORMS.PY
from .models import Auction
from django.forms import ModelForm
class AuctionForm(ModelForm):
class Meta:
model = Auction
fields = ['title', 'description', 'current_bid']
CREATE-LISTING.HTML
{% extends "auctions/layout.html" %}
{% block content %}
<form action="" method="post" >
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit">
</form>
{% endblock %}
CodePudding user response:
Your challenge comes from where you have imported form modelform. below is my solution.
from .models import Auction
from django import forms
class AuctionForm(forms.ModelForm):
class Meta:
model = Auction
fields = ['title', 'description', 'current_bid']
CodePudding user response:
Figured it out, the issue was I used {% block content %} instead of {% block body %}. That solved it. Thanks everyone
