Python Forum
Working on an Auction Site - had questions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Working on an Auction Site - had questions
#1
Hey guys I'm working on building an auction site for an assignment and I'm having issues with a few things.

The class "User" was set up for me. With an AbstractUser as a parameter. I'm assuming this is a built in value with Python that allows you to easily make a user class with username, password and name as the parameters. The only issue with this, is there's not a solid example to show us how to make our own class without a built in class like Abstract User.

I am trying to make an Auction class with the parameters of "Title", "Description", "Price", and "Category" in it.

However, whenever I try to pass title (instead of AbstractUser that was used for "User"), I get an error that title is not defined. I can type title = "hi" in the body of the class but how do I get title to equal whatever the user entered in the form?

I'm new with classes in Python and I am reading up about it but still cant' find a solution yet.

Below is my code.

Views.py
from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .models import User


def index(request):
    return render(request, "auctions/index.html")


def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
        else:
            return render(request, "auctions/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "auctions/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "auctions/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "auctions/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
    else:
        return render(request, "auctions/register.html")

def auction(request):  
    title = request.POST.get("title")
    description = request.POST.get("description")
    price = request.POST.get("price")
    category = request.POST.get("category")
    return render(request, "auctions/auction.html")


def watchlist(request):
    return render(request, "auctions/watchlist.html")

def categories(request):
    return render(request, "auctions/categories.html")
models:

from django.contrib.auth.models import AbstractUser
from django.db import models


class User(AbstractUser):
    pass

class Auction():
    

class Bid():
    pass

class Comment():
    pass
auction.html:

{% extends "auctions/layout.html" %}

{% block body %}
    <h2>Post Your Item For Sale</h2>
    
    <form
       name="auction"
       action="/auction"
       method="post"     >{% csrf_token %}
                <input type="text" name="title" placeholder="Enter Title of Item" required>
                <input type="textarea" size=100 name="description" placeholder="Enter Description of Item" required>
                <input type="number" name="price" placeholder="Enter Starting Price" required>
                <input type="text" name="category" placeholder="Enter Category" required>
                <input type="submit" id="auction" name="auction" value="Create New Auction" />
         </form>   

{% endblock %}
Reply
#2
anyone know the issue?
Reply
#3
Post the full error verbatim. At the very least, your models module should be causing a problem since line 9 is blank.
Reply
#4
OK here is my code so far :

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=64)
    description = models.CharField(max_length=64)
    price = models.IntegerField()
    category = models.CharField(max_length=64)

class Bid():
    pass

class Comment():
    pass
view.py:

def auction(request):  
    title = request.POST.get("title")
    description = request.POST.get("description")
    price = request.POST.get("price")
    category = request.POST.get("category")
    newPost = Auction.objects.create()
    newPost.save()
    return render(request, "auctions/auction.html")
I'm getting the error that "Auction" isn't defined. obbviously my syntax for the newPost = Auction.objects.create() is wrong. What am I doing wrong? When I put paramters - I get the same error. Thanks
Reply
#5
You haven't imported it?
Reply
#6
I have. For some reason the View is not recognizing the Model.
Reply
#7
Your view.py has no import statements whatsoever, so no, you haven't.
Reply
#8
oh it does I didn't include it all. Here is the whole thing :

from django.contrib.auth import authenticate, login, logout
from django.db import IntegrityError
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse

from .models import User


def index(request):
    return render(request, "auctions/index.html")


def login_view(request):
    if request.method == "POST":

        # Attempt to sign user in
        username = request.POST["username"]
        password = request.POST["password"]
        user = authenticate(request, username=username, password=password)

        # Check if authentication successful
        if user is not None:
            login(request, user)
            return HttpResponseRedirect(reverse("index"))
        else:
            return render(request, "auctions/login.html", {
                "message": "Invalid username and/or password."
            })
    else:
        return render(request, "auctions/login.html")


def logout_view(request):
    logout(request)
    return HttpResponseRedirect(reverse("index"))


def register(request):
    if request.method == "POST":
        username = request.POST["username"]
        email = request.POST["email"]

        # Ensure password matches confirmation
        password = request.POST["password"]
        confirmation = request.POST["confirmation"]
        if password != confirmation:
            return render(request, "auctions/register.html", {
                "message": "Passwords must match."
            })

        # Attempt to create new user
        try:
            user = User.objects.create_user(username, email, password)
            user.save()
        except IntegrityError:
            return render(request, "auctions/register.html", {
                "message": "Username already taken."
            })
        login(request, user)
        return HttpResponseRedirect(reverse("index"))
    else:
        return render(request, "auctions/register.html")

def auction(request):
    if request.method == "POST":
        title = request.POST.get("title")
        description = request.POST.get("description")
        price = request.POST.get("price")
        category = request.POST.get("category")
        new = NewPost.objects.create(title, description, price, category)
        new.save()
        return render(request, "auctions/auction.html")
    else:
        return render(request, "auctions/auction.html")


def watchlist(request):
    return render(request, "auctions/watchlist.html")

def categories(request):
    return render(request, "auctions/categories.html")
Reply
#9
You're still not importing Auction - line 7.
Reply
#10
wow thank you! It doesn't give me the error now.

Here is my code now. It doesn't give me an error but how can I test that it's actually putting the data into the db? The db doesn't exist in any format I know of where I can view it. Is there a way to print out the contents?

def auction(request):
    if request.method == "POST":
        title = request.POST.get("title")
        description = request.POST.get("description")
        price = request.POST.get("price")
        category = request.POST.get("category")
        new = NewPost.objects.create(title = title, description = description, price = price, category = category)
        new.save()
        return render(request, "auctions/auction.html")
    else:
        return render(request, "auctions/auction.html")
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020