1

私はこれに関する決定的な文書は見つかりませんでしたが、複数のビューでサイドバーで使用する必要がある連絡フォームがあります。現在、フォームを処理するために、複数のビューで以下のコードスニペットを繰り返しているため、私のコードは汚いです。 Post Requestを継承できるテンプレートに配置する方法はありますか?継承された投稿要求を持つ複数のビューで1つのDjangoフォーム

ビュー

def contact(request): 
    form_class = ContactForm 

    if request.method == 'POST': 
     form = form_class(data=request.POST) 
     messages.add_message(request, messages.SUCCESS, 'Thank you, we have received your message.') 

     if form.is_valid(): 
     ... 

あなたの助けをいただき、ありがとうございます。

def contact(request): 
    # First you choose the form. 
    form_class = ContactForm 

    # Then you want to know if request is POST type 
    if request.method == 'POST': 
     # You take the form data from given POST 
     form = form_class(data=request.POST) 
     # You add message to messages. 
     messages.add_message(request, messages.SUCCESS, 'Thank you, we have received your message.') 

あなたが同じオーバーを行うと、何度も繰り返した場合、あなたはの初めに、独自の機能を作成することができます。今、私はあなたがすべてのビューが実行されている間、次のように同じ操作を行うと仮定してい

+1

は、私はあなたが*本当に*フォームを継承*と言って何を意味するのか分からない:

def take_message(request, form, messages, message): # I'm reinitializing <form> variable here. form = form(data=request.POST) # That <message> variable below must be a string, then you can dynamically pass your message. messages.add_message(request, messages.SUCCESS, message) 

その後、私の見解は以下の通りである:だから、私は以下のようにtake_message方法があることに変化していますしかし、私はクラスベースのビューはあなたを助けるかもしれないと思う。 |しかし、上記のコードを必ずしも繰り返す必要はないという場合には、これはあなたを助けるかもしれません:https://codeshare.io/ZN7nx –

+0

Bloody brilliant !!数分待ってから '{'form':ContactForm}'を私の 'contact'関数に置かなければならないことに気付きました。私が正しいと受け入れる答えとしてコードシェアを置くことができますか?また、あなたが気にしないなら、あなたのコードの '(req、msg)'部分に関するいくつかのドキュメントを指摘できますか?私は検索しましたが、どこから来たのか分かりませんでした。再度、感謝します!! –

+0

これはドキュメント上には何もありません。単に関数を作成し、 'request'、' message'、 'form_class'を引数として渡しました。それで全部です。私はしばらく後で答えます。 –

答えて

1

あなたのviews.pyファイルを短くするアプリケーションと、not to repeat yourselfを何度も繰り返してください。

def take_message(request, form, messages, message): 
    if request.METHOD == "POST": 
     # I'm reinitializing <form> variable here. 
     form = form(data=request.POST) 
     # That <message> variable below must be a string, then you can dynamically pass your message. 
     messages.add_message(request, messages.SUCCESS, message) 

次に、あなたのビューでそれを使用します。

def contact(request): 
    take_message(request, ContactForm, messages, "Thanks, we got your message.") 
    # And the rest here. 

彼らは方法として、すべての要求の種類を扱うことができるので、あなたがclass-based viewsを使用するためしかし、私はお勧めします。 *

from django.views.generic import TemplateView 
# And any other important imports. 

# ... 

class ContactView(TemplateView): 
    template_name = "contact.html" # This is your template. 

    def get(self, request): 
     # Do things when the method is GET. Like, viewing current messages in a hypothetical admin template. 

    def delete(self, request): 
     # Do things when the method is DELETE. Don't forget to use authentication here, so only superuser can delete messages. 

    def post(self, request): 
     # Do things when the method is POST. 
     # I'm assuming anonymous users can send messages, so there's no need for authentication here. 
     take_message(request, ContactForm, messages, "Thanks you, we got your message.") 
     # Other things to do. 

# urls.py 
url(r"^contact/$", ContactView.as_view(), name="contact-page") 
関連する問題