如何解决错误消息:没有这样的列:auctions_auction.image_url
How to solve error message: no such column: auctions_auction.image_url
我正在创建一个拍卖网站,用户在创建新列表时会在该网站上提交带有 link 图像的表单。这个 link/url 然后将被转换并显示为网站上的图像。当我将包括 link 在内的表单详细信息上传到图像时,我一直收到此错误 -
""/create/
处出现操作错误
没有这样的列:auctions_auction.image_url。""
如何清除此错误?
VIEWS.PY
from django.shortcuts import render, redirect
from .forms import AuctionForm
def create_listing(request):
form = AuctionForm()
if request.method == 'POST':
form = AuctionForm(request.POST, request.FILES)
if form.is_valid:
form.save()
return redirect('index')
else:
form = AuctionForm()
context = {'form': form}
return render(request, 'auctions/create-listing.html', context)
FORMS.PY
from .models import Auction
from django import forms
from django.forms import fields
class AuctionForm(forms.ModelForm):
class Meta:
model = Auction
fields = ['title', 'description', 'current_bid', 'image_url']
MODELS.PY
from django.db import models
from django.core.files import File
from urllib.request import urlopen
from tempfile import NamedTemporaryFile
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)
image_upload = models.ImageField(upload_to='images/')
image_url = models.URLField(verbose_name="URL", max_length=255, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
if self.image_url and not self.image_upload:
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(urlopen(self.image_url).read())
img_temp.flush()
self.image_upload.save(f"image_{self.pk}", File(img_temp))
super(self).save(*args, **kwargs)
def __str__(self):
return self.title
HTML
{% extends "auctions/layout.html" %}
{% block body %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit">
</form>
{% endblock %}
URLS.PY
urlpatterns = [
path("create/", views.create_listing, name="create_listing")
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
您必须将模型更改迁移到数据库中。这可能有效:
$ python manage.py makemigrations
$ python manage.py migrate
我正在创建一个拍卖网站,用户在创建新列表时会在该网站上提交带有 link 图像的表单。这个 link/url 然后将被转换并显示为网站上的图像。当我将包括 link 在内的表单详细信息上传到图像时,我一直收到此错误 -
""/create/
处出现操作错误没有这样的列:auctions_auction.image_url。""
如何清除此错误?
VIEWS.PY
from django.shortcuts import render, redirect
from .forms import AuctionForm
def create_listing(request):
form = AuctionForm()
if request.method == 'POST':
form = AuctionForm(request.POST, request.FILES)
if form.is_valid:
form.save()
return redirect('index')
else:
form = AuctionForm()
context = {'form': form}
return render(request, 'auctions/create-listing.html', context)
FORMS.PY
from .models import Auction
from django import forms
from django.forms import fields
class AuctionForm(forms.ModelForm):
class Meta:
model = Auction
fields = ['title', 'description', 'current_bid', 'image_url']
MODELS.PY
from django.db import models
from django.core.files import File
from urllib.request import urlopen
from tempfile import NamedTemporaryFile
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)
image_upload = models.ImageField(upload_to='images/')
image_url = models.URLField(verbose_name="URL", max_length=255, unique=True)
created_at = models.DateTimeField(auto_now_add=True)
def save(self, *args, **kwargs):
if self.image_url and not self.image_upload:
img_temp = NamedTemporaryFile(delete=True)
img_temp.write(urlopen(self.image_url).read())
img_temp.flush()
self.image_upload.save(f"image_{self.pk}", File(img_temp))
super(self).save(*args, **kwargs)
def __str__(self):
return self.title
HTML
{% extends "auctions/layout.html" %}
{% block body %}
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{form.as_p}}
<input type="submit" value="Submit">
</form>
{% endblock %}
URLS.PY
urlpatterns = [
path("create/", views.create_listing, name="create_listing")
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT)
您必须将模型更改迁移到数据库中。这可能有效:
$ python manage.py makemigrations
$ python manage.py migrate