从URL模式或帖子数据将初始数据插入到SessionWizardView表单域中? [英] Inserting initial data into a SessionWizardView form field either from a URL pattern or post data?

查看:135
本文介绍了从URL模式或帖子数据将初始数据插入到SessionWizardView表单域中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



情景:


我已经用尽了这个问题的资源,所以我必须向社区提交一个问题。

在我的urls.py文件中,我有一个模式,如:

  url(r'^ entry_form / (?P< sheet_id> \d +)/',SheetWizard.as_view([Sheet1,Sheet2,Sheet3])),

当用户访问像127.0.0.1/myapp/entry_form/77这样的网址时,我试图让Django呈现Sheet1,但最初输入的值为77的字段之一。



Theroy:



我的forms.py文件看起来类似于:



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $((((((((((((((((((((((((((((((((((((((((((((( * args,** kwargs)
#create一个帮助对象
self.helper = FormHelper(self)
#dont呈现表单标签
self.helper.form_tag = False
#做一个漂亮的布局
self.helper.layout =布局(
TabHolder(
Tab(
'工作表信息',#Tab名称文本
PrependedText('sheet_field','Django'),#带前缀文本的字段


#Now将url模式的字段初始值设置为sheet_id
self.fields ['sheet_field']。initial = str(sheet_id)+' - ' + str(time()).replace('。','_')
#???不确定从哪里获取sheet_id?注意最后一行有一个名为sheet_id的变量,应该是值77来的从用户输入的url模式。



问题:



到目前为止,我不确定如何访问我的forms.py或views.py文件中的url模式的值sheet_id。由于这是一个基于类的视图,我不能简单地创建关键字sheet_id = None,也就是这样的东西不行:

  class SheetWizard(SessionWizardView,sheet_id = None):
#this break

我已经能够使用request.GET
和一个类似127.0.0.1/myapp/entry_form/?page_id=77的网址获取一些数据到views.py,但是我不知道如何将它导入到SessionWizardView用户会话的第一种形式。



如果有人能帮助我,我们将不胜感激。
感谢所有你的智慧!

解决方案

使用向导的 dispatch 方法:

  def dispatch(self,request,* args,** kwargs):
self .table_id = kwargs.get('sheet_id',无)
返回超级(SheetWizard,self).dispatch(request,* args,** kwargs)

然后在 self.sheet_id /dev/ref/contrib/formtools/form-wizard/#django.contrib.formtools.wizard.views.WizardView.get_form_initialrel =nofollow> get_form_initial 方法填充您的表单的初始值。


I've pretty much exhausted my resources on this subject, so I must submit a question to the community.

Scenario:

In my urls.py file i have a pattern like:

url(r'^entry_form/(?P<sheet_id>\d+)/', SheetWizard.as_view([Sheet1,Sheet2,Sheet3])),

When a user visits a url like "127.0.0.1/myapp/entry_form/77" I am trying to have Django render Sheet1 but with one of the fields having the value "77" initially entered.

Theroy:

My forms.py file looks similar to:

class Sheet1(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super(Sheet1, self).__init__(*args, **kwargs)
        #create a helper object
        self.helper = FormHelper(self)
        #dont render the form tags
        self.helper.form_tag = False
        #Make a nice layout
        self.helper.layout = Layout(
             TabHolder(
                             Tab(
                                 'Sheet Information', #Tab name text
                                     PrependedText('sheet_field', 'Django-'), #The field with prepended text
                             )
             )
        #Now set the initial value of the field to sheet_id from url pattern
        self.fields['sheet_field'].initial = str(sheet_id)+'-'+ str( time() ).replace('.','_')
        #??? Not sure where to get sheet_id from???

Notice the last line has a variable named "sheet_id", that should be the value "77" coming from the url pattern entered by the user.

Problem:

So far I am unsure of how to access the value "sheet_id" from the url pattern in my forms.py or views.py files. Due to this being a class based view I can not simply create the keyword "sheet_id=None", aka something like this just doesn't work:

class SheetWizard(SessionWizardView, sheet_id=None):
    #this breaks

I have been able to get some data into views.py using request.GET and a url like "127.0.0.1/myapp/entry_form/?sheet_id=77" but I have no idea how to pipe that into the first form of the SessionWizardView user session.

It would be greatly appreciated if someone could help me out. And thanks for all your kind wisdom!

解决方案

Use the wizard's dispatch method:

def dispatch(self, request, *args, **kwargs):
    self.sheet_id = kwargs.get('sheet_id', None)
    return super(SheetWizard, self).dispatch(request, *args, **kwargs)

Then use self.sheet_id within get_form_initial method to populate initial value for your form.

这篇关于从URL模式或帖子数据将初始数据插入到SessionWizardView表单域中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆