简体   繁体   English

将视图与模板视图django合并

[英]merging a view with template view django

I want that the landing page of my homepage is a form with an input and the user puts in stuff. 我希望主页的登录页面是带有输入内容的表单,并且用户放入东西。 So I followed a couple of tutorials and now I have this: 所以我遵循了一些教程,现在我有了:

views.py: views.py:

def create2(request): 
  if request.method =='POST':
    form = LocationForm(request.POST)
    if form.is_valid():   
      form.save()
      return HttpResponseRedirect('')
  else:
    form = LocationForm()

  args = {}
  args.update(csrf(request))
  args['form'] = form
  return render_to_response('location/index.html', args)

and in my urls.py: 在我的urls.py中:

url(r'^$', 'core.views.create2'),

which works perfectly fine, if I go to 127.0.0.1:8000 I get to index.html and when put in something in the input it gets saved in the database. 如果我转到127.0.0.1:8000,它可以很好地工作,如果我进入index.html并在输入中输入内容,它将保存在数据库中。 However, the old part of my homepage looks like this 但是,我主页的旧部分看起来像这样

class LandingView(TemplateView):
    model = Location
    template_name="location/index.html"
    def search
    ...

and the urls.py: url(r'^$', core.views.LandingView.as_view(), name='index') , url(r'^$', core.views.LandingView.as_view(), name='index')

which has a function search I So my question is, is there a way how I can merge the def create2 into my LandingView . 其中有一个功能search我所以我的问题是,有没有一种方法,我怎么能合并def create2到我LandingView I tried several things, but I am always ending up having the index.html without the input field. 我尝试了几件事,但最终总是得到没有输入字段的index.html。 I also tried 我也试过

def create2 
...
def search
...

but didn't work. 但是没有用

Does anyone know how to merge that together? 有谁知道如何将它们合并在一起?

EDIT Thank you the working solution looks like this now 编辑谢谢您的工作解决方案现在看起来像这样

class Create(CreateView):
  model = coremodels.Location
  template_name = 'location/test.html'
  fields = ['title']
  def form_valid(self, form):
     form.save()
     return HttpResponseRedirect('')

    return super(Create, self).form_valid(form) 

Depending on the results you are looking for, there are multiple ways to solve this: 根据您寻找的结果,有多种解决方法:

1. Use CreateView and UpdateView 1.使用CreateViewUpdateView

Django already provides some classes that render a form for your model, submit it using POST , and re-render the form with errors if form validation was not successful. Django已经提供了一些类,这些类可以为您的模型呈现表单,使用POST提交表单,如果表单验证不成功,则使用错误重新呈现表单。

Check the generic editing views documentation. 查看常规编辑视图文档。

2. Override get_context_data 2.覆盖get_context_data

In LandingView , override TemplateView 's get_context_data method, so that your context includes the form you are creating in create2 . LandingView ,重写TemplateViewget_context_data方法,以便您的上下文包括您在create2中创建的create2

3. Use FormView 3.使用FormView

If you still want to use your own defined form instead of the model form that CreateView and UpdateView generate for you, you can use FormView , which is pretty much the same as TemplateView except it also handles your form submission/errors automatically. 如果仍然要使用自己定义的表单而不是CreateViewUpdateView为您生成的模型表单,则可以使用FormView ,它与TemplateView几乎相同,不同之处在于它还可以自动处理表单提交/错误。


In any case, you can keep your search function inside the class-based view and call it from get_context_data to include its results in the template's context. 无论如何,您都可以将search功能保留在基于类的视图中,然后从get_context_data调用它,以将其结果包含在模板的上下文中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM