質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.41%
Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

540閲覧

Djangoで return redirect で frontpage を指定しているのですが、editに使用したリンクにリダイレクトされます。

alizona

総合スコア126

Django

DjangoはPythonで書かれた、オープンソースウェブアプリケーションのフレームワークです。複雑なデータベースを扱うウェブサイトを開発する際に必要な労力を減らす為にデザインされました。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2023/01/21 22:20

編集2023/01/21 22:23

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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

path('openpricerequesttoowner/int:pk/', views.OpenPriceRequestToOwner, name='openpricerequesttoowner'),

int:pk の後ろに、/ で アップデートページに戻れたのでよしとします。

投稿2023/01/22 00:23

alizona

総合スコア126

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.41%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問