已关闭此问题为not reproducible or was caused by typos。它目前不接受回答。
此问题是由打印错误或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这一个是解决的方式不太可能帮助未来的读者。
昨天关门了。
Improve this question
我试图使一个电子商务网站,但它不会让我创建的上市,并张贴在这里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, Category, Listing
def index(request):
activeListings = Listing.objects.filter(isActive=True)
allCategories = Category.objects.all()
return render(request, "auctions/index.html",{
"listings": activeListings,
"categories": allCategories
})
def displayCategory(request):
if request.method == "POST":
categoryFromForm = request.POST['category']
category = category.objects.get(categoryName=categoryFromForm)
activeListings = Listing.objects.filter(isActive=True, category=category)
allCategories = Category.objects.all()
return render(request, "auctions/index.html",{
"listings": activeListings,
"categories": allCategories
})
def createListing(request):
if request.method=="GET":
allCategories = Category.objects.all()
return render(request, "auctions/create.html", {
"categories": allCategories
})
else:
title = request.POST.get("title")
description = request.POST.get("description")
imageurl = request.POST.get("imageurl")
price = request.POST.get("price")
category = request.POST.get("category")
currentUser = request.user
categoryData=Category.objects.get(categoryName=category)
newListing = Listing(
title = title,
description = description,
imageUrl = imageurl,
price = str(price),
category = categoryData,
owner = currentUser
)
newListing.save()
return HttpResponseRedirect(index)
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")
网址:models.py
from django.db import models
class User(AbstractUser):
pass
class Category(models.Model):
categoryName = models.CharField(max_length=50)
def __str__(self):
return self.categoryName
class Listing(models.Model):
title = models.CharField(max_length=30)
imageUrl = models.CharField(max_length=300)
price = models.FloatField(max_length=1000)
isActive = models.BooleanField(default=True)
owner = models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, related_name="user")
category = models.ForeignKey(Category, on_delete=models.CASCADE, blank=True, null=True, related_name="category")
def __str__(self):
return self.title
hereindex.html
{% extends "auctions/layout.html" %}
{% block body %}
<h2>Active Listings</h2>
<form action="{% url 'displayCategory' %}" method="POST">
{% csrf_token %}
<label for="category">Choose a category:</label>
<select name="category" id="category">
{% for cat in categories %}
<option value="{{ cat }}">{{ cat }}</option>
{% endfor %}
</select>
<button type="submit" class="btn btn-success">Select</button>
</form>
<div class="row mx-3">
{% for listing in listings %}
<div class="card" style="width: 18rem;">
<img class="card-img-top" src="{{ listing.imageUrl }}" alt="{{ listing.title }}">
<div class="card-body">
<h5 class="card-title">{{ listing.title }}</h5>
<p class="card-text">{{ listing.description }}</p>
<a href="" class="btn btn-primary"></a>
</div>
</div>
{% endfor %}
</div>
{% endblock %}
这里是create.html
{% extends "auctions/layout.html" %}
{% block body %}
<h2>Create Listing</h2>
<form action="{% url 'create' %}" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" placeholder="Enter Title:">
</div>
<div class="form-group">
<label for="Description">Description</label>
<input type="text" class="form-control" id="description" placeholder="Enter description:">
</div>
<div class="form-group">
<label for="imageurl">Image</label>
<input type="text" class="form-control" id="imageurl" placeholder="Enter image url:">
</div>
<div class="form-group">
<label for="price">Price</label>
<input type="text" class="form-control" id="price" placeholder="Enter Price">
</div>
<div class="form-group">
<label for="category">Choose a category:</label>
<select name="category" id="category">
{% for cat in categories %}
<option value="{{ cat }}">{{ cat }}</option>
{% endfor %}
</select>
</div>
<button type="submit" class="btn btn-success">Add listing</button>
</form>
{% endblock %}
我不知道该怎么办,但问题就在这里
title = request.POST.get("title")
description = request.POST.get("description")
imageurl = request.POST.get("imageurl")
price = request.POST.get("price")
category = request.POST.get("category")
currentUser = request.user
categoryData=Category.objects.get(categoryName=category)
newListing = Listing(
…
title = title,
description = description,
imageUrl = imageurl,
price = str(price),
category = categoryData,
owner = currentUser
我不知道发生了什么事,请帮助它的一个cs50W项目称为商业,如果你们中的任何人做了。
1条答案
按热度按时间b5buobof1#
您的
Listing
型号没有description
字段。如果您真的希望创建一个带有描述的清单,那么就向您的Listing
模型添加一个描述字段并迁移更改。如果不需要描述,请将其从views.py
createListing
方法中删除。