简体   繁体   中英

Creating Generic Views in Django

Ok so I am new to Django but not to the MVC world (built a few apps in Grails)...but I seem to be having the same issue as a bunch of people and I am hoping someone can shed some real light on Django's Class based generic views. In the Grails world "templates" or generic views CAN BE generated by the framework and I believe the same thing is true regarding Django but if you follow the 1.7 "Writing your first django app" there seems to be some special magic missing.

I have the following project/app structure:

mysite
     ->mysite
           ->settings.py
           ->urls.py
           ->_init_.py
           ->wsgi.py
      ->polls (my app)
            ->models.py
            ->urls.py
            ->views.py
            ->templates
                ->polls

In the tutorial we are asked to created an index.html, details.html, results.html file in the templates->polls directory and we have some simply HTML views within those files. That all works great. Once I get to "Use generic views: Less code is better" in the tutorial is where I get into trouble. Am I wrong in assuming I should simply REMOVE the index.html, details.html, results.html files from the templates folder AND that Django should be generating views on the fly? I assumed that was the point of the generic views but now I am not sure. When I do remove the views I get a templatedoesnotexist error. Do I need to do something to my settings.py to let it know that I want to look to django to generate the views? The following is the standard generated settings (which is what I have) and I assume that should be sufficient to perform default behaviors?

 import os
 BASE_DIR = os.path.dirname(os.path.dirname(__file__))
 TEMPLATE_DEBUG = True

 TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates/'),]

If you need more from me let me know, but basically I am wondering if I am way off on assuming Django generates the views from a template or not? Thank you!

You're just a bit confused about Django's terminology. Almost all views use a template to render the actual markup (HTML, XML, whatever) that is sent back to the browser.

A Django Generic View is just a Class based version of writing your own functional view. So instead of something like:

def show_template_with_data(request):
    template = "myapp/show_data.html"
    return render(request, template, {'data1': 1, 'data2': 2})

you would use Python classes and OO and do:

class ShowTemplateWithData(TemplateView):
    template_name = "myapp/show_data.html"

    def get_context_data(self, **kwargs):
        context = super(ShowTemplateWithData, self).get_context_data(**kwargs)
        context['data1'] = 1
        context['data2'] = 2
        return context 

These two are equivalent one is just using functions and the other Class Based Views.

In Django, views are the code that determines what action is taken, what data is given to the template (for example based on logged in user), etc. a "template" is just the HTML with placeholders/loops/etc that displays it.

But no, you shouldn't be deleting the template HTML files, both styles of views will need them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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