2013-02-20 1 views
2

Userena를 사용하고 있으며 URL 매개 변수를 캡처하여 양식으로 가져 오려고하지만이를 수행하는 방법이 없습니다.형태로 캡처 된 URL 매개 변수

내가 내 템플릿에 어떻게하고 싶은 것은 내 urls.py에서 다음

<a href="/accounts/signup/freeplan">Free Plan</a><br/> 
<a href="/accounts/signup/proplan">Pro Plan</a><br/> 
<a href="/accounts/signup/enterpriseplan">Enterprise Plan</a><br/> 

그리고

url(r'^accounts/signup/(?P<planslug>.*)/$','userena.views.signup',{'signup_form':SignupFormExtra}), 

다음, 이상적으로, 나는에 그 planslug을 사용하고 싶습니다 내 forms.py를 사용하여 프로필에 사용자 계획을 설정합니다.

캡쳐 된 URL 매개 변수를 사용자 정의 양식으로 가져 오는 방법이 손실됩니다. extra_context를 사용할 수 있습니까? Userena 가입보기를 재정의해야합니까?

답변

1

당신은 사용하여 템플릿에 URL을 액세스 할 수 있습니다 -

{% request.get_full_path %} 

을 (더 많은 정보에 대한 docs 참조).

그러나 당신은 단지 다음 planslug 변수를 얻을 템플릿 뷰에서 그것을 전달하고 템플릿에 액세스하려는 경우 (이 명명 된 매개 변수는 URL에 있기 때문에 그것은 뷰에서 사용할 수) -

def signup(request, planslug=None): 
    # 
    render(request, 'your_template.html', {'planslug':planslug} 

과는 템플릿에 당신은 그것을 얻을 -

{% planslug %} 

당신이 템플레이트에 전달하기 전에 당신이 당신의 상황에 planslug 변수를 추가 override get_context_data해야합니다 클래스 기반의 뷰를 사용하는 경우

def get_context_data(self, *args, **kwargs): 
    context = super(get_context_data, self).get_context_data(*args, **kwargs) 
    context['planslug'] = self.kwargs['planslug'] 
    return context 
6

클래스 기반보기를 사용하는 경우 FormMixin 클래스의 def get_form_kwargs() 메소드를 덮어 쓸 수 있습니다. 여기서 모든 매개 변수를 양식 클래스에 전달할 수 있습니다. urls.py에서

가 : views.py에서

url(r'^create/something/(?P<foo>.*)/$', MyCreateView.as_view(), name='my_create_view'), 

: forms.py에서

class MyCreateView(CreateView): 
    form_class = MyForm 
    model = MyModel 

    def get_form_kwargs(self): 
     kwargs = super(MyCreateView, self).get_form_kwargs() 
     # update the kwargs for the form init method with yours 
     kwargs.update(self.kwargs) # self.kwargs contains all url conf params 
     return kwargs 

:

class MyForm(forms.ModelForm): 

    def __init__(self, foo=None, *args, **kwargs) 
     # we explicit define the foo keyword argument, cause otherwise kwargs will 
     # contain it and passes it on to the super class, who fails cause it's not 
     # aware of a foo keyword argument. 
     super(MyForm, self).__init__(*args, **kwargs) 
     print foo # prints the value of the foo url conf param 

희망이 도움이 :-)

+0

그리고 클래스 기반 뷰를 사용하지 않는다면? –