我在尝试加载表单时遇到了请求上下文错误.
views.py
def add_company(request):
# Get the context from the request.
context = RequestContext(request)
# A HTTP POST?
if request.method == 'POST':
form = CompanyForm(request.POST)
# Have we been provided with a valid form?
if form.is_valid():
# Save the new category to the database.
form.save(commit=True)
# Now call the index() view.
# The user will be shown the homepage.
return index(request)
else:
# The supplied form contained errors - just print them to the terminal.
print form.errors …Run Code Online (Sandbox Code Playgroud) 我用 CompanyCreateView 创建了一个新模型。用 保存后f.save(),我希望浏览器加载成功 url
url(r'^comp/(?P<pk>\w+)/$', CompanyDetailView.as_view(), name="profile"),
Run Code Online (Sandbox Code Playgroud)
这是我的 CreateView。
class CompanyCreateView(CreateView):
model = Company
form_class = CompanyForm
success_url = "/comp/???pk???"
def form_valid(self, form):
f = form.save(commit=False)
f.submitter_id = 99 #dont know how to remove the submitter, its not set in the model
f.save()
return super(CreateView, self).form_valid(form)
def get_success_url(self, **kwargs):
return reverse("profile", kwargs={'pk': self.request.pk})
Run Code Online (Sandbox Code Playgroud)
如何使用 args 参数返回pk?