简体   繁体   English

如何使用视图数据呈现HTML模板?

[英]How to render an html template with data from view?

I am new to django. 我是django的新手。 I made a form. 我做了一个表格。 I want that if the form is filled successfully then django should redirect to a success page showing the name entered in the form but no parameters should be present in the url itself. 我希望如果成功填写了表单,那么django应该重定向到显示在表单中输入名称的成功页面,但URL本身中不应该存在任何参数。

I searched on the internet and the solution I got was to redirect to url with pk as a get parameter which fetches the data and shows in the view. 我在互联网上搜索,得到的解决方案是使用pk作为获取参数重定向到url,该参数获取数据并显示在视图中。 But I don't want to pass any thing in the url itself. 但是我不想在url本身中传递任何东西。 and some websites say that http can't redirect with post data. 有些网站说http无法重定向帖子数据。

Here's my views.py 这是我的views.py

class UserRegistrationView(CreateView):
model = UserForm
template_name = 'userregistration.html'
form_class = UserForm
success_url = 'success'

def get_success_url(self):
    return reverse('success',kwargs = {'name' : self.object.firstName})

and here's the template to which I want to redirect: 这是我要重定向到的模板:

<h2>Congratualations for registering {{name}} </h2>

Basically what I want is that if the person fill form mentioning his/her firstName as "xyz" then the redirected success page should say that "Congratulations for registering xyz" 基本上,我想要的是,如果该人填写的表格中提到他/她的名字为“ xyz”,那么重定向成功页面应显示“恭喜您注册xyz”

You can use django sessions, which I believe installed by default in 1.8 您可以使用django会话,我相信它默认安装在1.8中

Look here 这里

# Set a session value:
request.session["fav_color"] = "blue"

# Get a session value -- this could be called in a different view,
# or many requests later (or both):
fav_color = request.session["fav_color"]

# Clear an item from the session:
del request.session["fav_color"]

You can pass your pk via session and extract your object in another view without affecting your url. 您可以通过会话传递pk并在另一个视图中提取对象,而不会影响您的网址。 Make sure you clean up after yourself. 确保自己清理干净。

Let me know if more help needed. 让我知道是否需要更多帮助。

One of the possible ways of passing data between views is via sessions. 在视图之间传递数据的一种可能方式是通过会话。 So, in your UserRegistrationView you need to override the form_valid method as shown below. 因此,在您的UserRegistrationView中,您需要重写form_valid方法,如下所示。

class UserRegsitrationView(CreateView):
    def form_valid(self,form):
        self.request.session['name'] = self.object.firstName
        return super(UserRegistrationView,self).form_valid(form)



class SuccessView(TemplateView):
    template_name = "success_template.html"

    def get_context_data(self,**kwargs):
        context = super(SuccessView,self).get_context_data(**kwargs)
        context['name'] = self.request.session.get('name')
        del self.request.session['name']
        return context 

One more thing that you can modify in your code is that you need not declare success_url if you are overriding get_success_url 你可以在你的代码修改一件事是,你不必声明success_url如果要覆盖get_success_url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM