Djangoで、関数ベースのviewを作成しました。 フォームを送信してredirectしたいのですが、
リダイレクト先が、index.html ( namespace='frontpaga') になりません。
また、formの値も更新されません。
エラー内容はこちらです。
error
1Page not found (404) 2Request Method: POST 3Request URL: https://webpage.herokuapp.com/openpricerequesttoowner/
herokuapp.com/ にリダイレクトしたいです。
お手数ですがアドバイスをいただけないでしょうか?
views.py
python
1def OpenPriceRequestToOwner(request, pk): 2 post=Post.objects.get(pk=pk) 3 if request.method == 'POST': 4 form = OpenPriceRequestToOwnerForm(request.POST) 5 6 if form.is_valid(): 7 comment=form.save(commit=False) 8 comment.post=post 9 10 comment.requestpricelog= comment.requestpricelog + '\n'+ str(dt_now = datetime.datetime.now())+ ' ' +request.user.get_username + ' requested to change price to ' + str(comment.requestprice) +'\n comment: '+ comment.requestpricecomment 11 new_pricestatus=PriceStatus.objects.get(pricestatus="askingtoowner") 12 comment.pricestatus=new_pricestatus 13 14 comment.save() 15 # return redirect("openpricerequesttoowner",pk=post.pk) 16 return redirect("registration:frontpage") 17 else: 18 form=OpenPriceRequestToOwnerForm() 19 20 return render(request, "registration/openpricerequesttoowner.html", {"post":post, "form":form}) 21 22 23
urls.py
python
1 2urlpatterns=[ 3 path('', include("django.contrib.auth.urls")), 4 5 path("",frontpage,name="frontpage"), 6 7 path("<int:pk>/", post_detail, name="post_detail"), 8 9 path('update/<pk>/', views.Update.as_view(), name="update"), 10 path('delete/<pk>/', views.Delete.as_view(), name="delete"), 11 12 path('openpricerequesttoowner/<int:pk>', views.OpenPriceRequestToOwner, name='openpricerequesttoowner'), 13 14] 15
forms.py
python
1class OpenPriceRequestToOwnerForm(forms.ModelForm): 2 class Meta: 3 model=Post 4 fields=["requestprice","requestpricecomment"]
models.py
python
1 2class Post(models.Model): 3 title=models.CharField(max_length=255) 4 due= models.DateTimeField( 5 auto_now=False, 6 editable=True, 7 blank=False, 8 null=False) 9 fromwho = models.CharField(max_length=20) 10 11 towho = models.ForeignKey( 12 Towho, 13 on_delete=models.CASCADE, 14 default=8) 15 16 body=models.TextField() 17 posted_date=models.DateTimeField(auto_now_add=True) 18 image = CloudinaryField('image', null=True, blank=True) 19 video = CloudinaryField('video', null=True, blank=True) 20 21 status = models.ForeignKey( 22 Status, 23 verbose_name='状況', on_delete=models.PROTECT, 24 default=5) 25 26 price=models.IntegerField(default=50) 27 28 pricestatus = models.ForeignKey( 29 PriceStatus, 30 verbose_name='支払い状況', on_delete= models.PROTECT, 31 default=2 32 ) 33 34 requestprice = models.IntegerField(default=0) 35 requestpricecomment = models.TextField(default='') 36 requestpricelog = models.TextField(default='') 37
openpricerequesttoowner.html
pytho
1{% extends "registration/base.html" %} 2{% block content %} 3 4 5 6 <!------------------post placed-------------> 7 <div class="post block"> 8 <br> 9 <br> 10 <h2 class="Send price request to owner" >{{post.title}}</h2> 11 <br> 12 <br> 13 <h3>Current Price: {{post.price}} $.</h3> 14 </div> 15 16 <br> 17 <br> 18 <p>Price request history</p> 19 <br> 20 <br> 21 {{post.requestpricelog}} 22 <br> 23 24 <h3 class="subtitle">Price Change Request</h3> 25 <br> 26 <form action="." method="post" enctype="multipart/form-data"><!-- これは画像投稿よう--> 27 <!-- add photo function here later --> 28 {% csrf_token%} 29 {{form.as_p}} 30 <div class="field"> 31 <div class="control"> 32 <button class="button is-danger">Submit</button> 33 34 </div> 35 </div> 36 </form> 37 38 39{% endblock %} 40

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。