Sep-05-2020, 10:37 PM
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
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(): passauction.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 %}