message.success不显示消息django

时间:2019-02-20 18:09:48

标签: python django

我是django的新手,我一直试图在用户成功添加项目时显示成功消息。我正在使用message.success,但似乎无法正常工作。其他应用程序,但它可以正常工作,不知道为什么在这种情况下不起作用。 Views.py:

from django.shortcuts import render
from django import views
from .forms import project_form
from django.shortcuts import render,redirect,HttpResponse
from apps.profiles.models import profiles
from django.contrib import messages

class Add_project(views.View):
def get(self,request,*args,**kwargs):
    loggeduser=profiles.objects.get(Username__username=request.user)
    return render(
    request,
    'form.html',
    context={
        'form': project_form(initial={"by_username":loggeduser})
    }
    )

def post(self,request,*args,**kwargs):
    form=project_form(request.POST)
    if form.is_valid():
        form.save()
        messages.success(request,('Project saved successfully'))
        return redirect('projects:AddProject')
       # projects.objects.get(by_username__username=request.user)

    else:
        return HttpResponse(
            '''
            <html><body><h1>Not valid'''
            )

form.html

<html>
<body>
    <form action="" method="post" enctype="multipart/form-data">
        {% csrf_token %}
        {{error}}
        {{ form }}
        {% if messages %}
        <ul class="messages">
        {% for message in messages %}
        <li class="{{ message.tags }}">{{ message }}</li>
        {% endfor %}
        </ul>
        {% endif %}
        <br>
        <br>
        <input type="submit" value="Submit">
    </form>

</body>
</html>

urls.py

from django.urls import path
from .views import Add_project,project_list
urlpatterns = [
path('addproject/',Add_project.as_view(),name='AddProject'),
]

重定向后,页面完美显示表单,但没有出现该消息。请帮帮我。

0 个答案:

没有答案