小编ora*_*j33的帖子

Django'RequestContext'未定义 - forms.ModelForm

我在尝试加载表单时遇到了请求上下文错误.

  1. 在我的models.py上创建了ModelForm
  2. 在我的视图上创建了def添加
  3. 链接到视图的URL

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)

python forms django django-forms

4
推荐指数
2
解决办法
9877
查看次数

如何从保存的模型中使用 pk 提供成功 url?

我用 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

python django url args

3
推荐指数
1
解决办法
1954
查看次数

标签 统计

django ×2

python ×2

args ×1

django-forms ×1

forms ×1

url ×1