Issue
I am new to Django and working on forms. I got this error message
File "C:\Users\foo\what\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\foo\what\lib\site-packages\django\utils\deprecation.py", line 136, in __call__
response = self.process_response(request, response)
File "C:\Users\foo\what\lib\site-packages\django\middleware\common.py", line 108, in process_response
if response.status_code == 404 and self.should_redirect_with_slash(request):
AttributeError: 'Product' object has no attribute 'status_code'
models.py
class Product(models.Model):
product_name = models.CharField(max_length=100)
product_price = models.IntegerField()
product_photo = models.ImageField(upload_to='product_photos', height_field=None, width_field=None, max_length=100)
def get(self, request):
a = Product()
return a
forms.py
class UploadProduct(ModelForm):
product_name = forms.TextInput()
product_price = forms.NumberInput()
product_photo = forms.FileInput()
class Meta:
model = Product
fields = ['product_name', 'product_price', 'product_photo']
views.py
def uploadProduct(request):
if request.POST:
form = UploadProduct(request.POST)
if form.is_valid():
form.save()
return redirect(upload_product_ok_page)
return render(request, 'upload_product.html', {'form': UploadProduct})
def upload_product_ok_page(request):
return HttpResponse('ok')
urls.py
path('upload_product/', Product),
path('upload_product/', views.uploadProduct, name="upload_product"),
path('', views.upload_product_ok_page, name='upload_product_ok_page'),
upload_product.html
<form method="POST" action="{% url 'upload_product' %}" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<button>Submit</button>
</form>
I made sure I imported the correct functions, classes and objects. Thanks in advance.
Solution
fro what i'm seeing you have some unnecessary urls here, i think you should remove the following
path('upload_product/', Product),
path('', views.upload_product_ok_page, name='upload_product_ok_page'),
especially
path('upload_product/', Product),
Answered By - Joshua Edward
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.