이 페이지에서 fantastic http://ccbv.co.uk/의 CreateView page에 관심이있을 수 있습니다, 당신은 멤버 메소드와 변수가 사용할 수있는 한 눈에 볼 수 있습니다. 귀하의 경우에는
, 당신은 무시할 관심을 가질 것입니다 :
def get_initial(self):
# Call parent, add your slug, return data
initial_data = super(ApplicationCreateView, self).get_initial()
initial_data['slug'] = ... # Not sure about the syntax, print and test
return initial_data
def get_context_data(self, **kwargs):
# Call parent, add your job object to context, return context
context = super(ApplicationCreateView, self).get_context_data(**kwargs)
context['job'] = ...
return context
이는 전혀 테스트되지 않았습니다. 조금만 가지고 놀아야 할 수도 있습니다. 재미있게 보내십시오. dispatch` 컨텍스트에 데이터를 추가 할 수있는 권리 장소가 아니다`좋은
class ApplicationCreateView(CreateView):
model = Application
form_class = ApplicationForm
success_url = 'submitted/'
def dispatch(self, *args, **kwargs):
self.job = get_object_or_404(Job, slug=kwargs['slug'])
return super(ApplicationCreateView, self).dispatch(*args, **kwargs)
def form_valid(self, form):
#Get associated job and save
self.object = form.save(commit=False)
self.object.job = self.job
self.object.save()
return HttpResponseRedirect(self.get_success_url())
def get_context_data(self, *args, **kwargs):
context_data = super(ApplicationCreateView, self).get_context_data(*args, **kwargs)
context_data.update({'job': self.job})
return context_data
출처
2014-02-20 03:29:03
Nil
을 당신이 작동하는지 알고 있지만 : – Nil
내 코드를 어떻게 수정 하시겠습니까? –
내가 제안한 것을 시도 했습니까? 너는 대답하지 않았다. Afaik, 그들은 과부하하는 올바른 방법입니다. – Nil