Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic MVT issue in Django

I have a Django website as follows:

  • site has several views
  • each view has its own template to show its data
  • each template extends a base template
  • base template is the base of the site, has all the JS/CSS and the basic layout

So up until now it's all good. So now we have the master head of the site (which exists in the base template), and it is common to all the views.

But now I want to make it dynamic, and add some dynamic data to it. On which view do I do this? All my views are basically render_to_response('viewtemplate.html', someContext). So how do add a common view to a base template?

Obviously I will not duplicate the common code to each separate view...

I think I'm missing something fundamental in the MVT basis of Django.

like image 903
Yuval Adam Avatar asked Apr 24 '09 14:04

Yuval Adam


People also ask

How does MVT work in Django?

Django is based on MVT (Model-View-Template) architecture. MVT is a software design pattern for developing a web application. View: The View is the user interface — what you see in your browser when you render a website. It is represented by HTML/CSS/Javascript and Jinja files.

What is MVT template Django?

The Template is a presentation layer which handles User Interface part completely. The View is used to execute the business logic and interact with a model to carry data and renders a template. Although Django follows MVC pattern but maintains it? s own conventions. So, control is handled by the framework itself.

What is MVT and advantages?

Model View Template (MVT) : This Model similar to MVC acts as an interface for your data and is basically the logical structure behind the entire web application which is represented by a database such as MySql, PostgreSQL. The View executes the business logic and interacts with the Model and renders the template.

What does {% %} mean in Django?

{% %} and {{ }} are part of Django templating language. They are used to pass the variables from views to template. {% %} is basically used when you have an expression and are called tags while {{ }} is used to simply access the variable.


1 Answers

You want to use context_instance and RequestContexts.

First, add at the top of your views.py:

from django.template import RequestContext

Then, update all of your views to look like:

def someview(request, ...)
    ...
    return render_to_response('viewtemplate.html', someContext, context_instance=RequestContext(request))

In your settings.py, add:

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.core.context_processors.auth',
    ...
    'myproj.app.context_processors.dynamic',
    'myproj.app.context_processors.sidebar',
    'myproj.app.context_processors.etc',
)

Each of these context_processors is a function takes the request object and returns a context in the form of a dictionary. Just put all the functions in context_processors.py inside the appropriate app. For example, a blog might have a sidebar with a list of recent entries and comments. context_processors.py would just define:

def sidebar(request):
    recent_entry_list = Entry.objects...
    recent_comment_list = Comment.objects...
    return {'recent_entry_list': recent_entry_list, 'recent_comment_list': recent_comment_list}

You can add as many or as few as you like.

For more, check out the Django Template Docs.

like image 105
tghw Avatar answered Sep 27 '22 02:09

tghw