Views and render () in Django

3

I am learning to use Django (specifically version 1.9). However, when creating a view, and when starting the Django server, I have the following error:

TypeError: render() takes at least 2 arguments (1 given)

What I do not understand is, what other parameters should the render() method have, apart from the direction of the view (considering it is static)? This error was corrected with the addition of missing parameters. However, now I am presented with the following error, which I do not know why it occurs:

AttributeError: 'str' object has no attribute 'regex'

In my views.py file of my application, I have the following (CORRECTED):

from django.shortcuts import render
from django.template import Template

def index(request):
    template_name = '/app/web/index.html'
    return render(request, template_name)

Regarding my URL file, it has the following:

from django.conf.urls import include, url
from django.contrib import admin
import app.views

urlpatterns = ['',
    # Vista para pagina de inicio
    url(r'^$', app.views.index),
    # Otras vistas
    url(r'^admin/', include(admin.site.urls)),
    ]'

And in the configuration file, I partially edited the templates section, adding the folder where my page is in:

# Application definition
INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'app'
    )

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    )

ROOT_URLCONF = 'vehiculos.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            '/app/web',
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'vehiculos.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'vehiculos',
        'USER': 'root',
        'PASSWORD': '',
        # 'HOST': 'localhost'
        # 'PORT': '3306',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'es-CL'
TIME_ZONE = 'America/Argentina/Mendoza'
USE_I18N = True
USE_L10N = True
USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

Thank you very much in advance

    
asked by KPavezC 15.12.2015 в 20:51
source

1 answer

1

The render function should receive the request as the first parameter of the view that is going to be rendered:

def index(request):
    template_name = '/app/web/index.html'
    return render(request, template_name)

The request and the name of your template ( template_name ) are the minimum parameters that the render function should receive. It would be good to explain the reason why you defined your function in that way, taking as a parameter of the view the template . This is wrong:

def index(template='/app/web/index.html'):
    return render(template)'

views are features that get request and return a response or response .

Regarding the URL:

url(r'^$', app.views.index())

You should not call the function, just pass the reference (without using parentheses):

url(r'^$', app.views.index)

You can also use a string with the path to the function and with this you avoid importing app :

url(r'^$', 'app.views.index')

Update

Apparently the problem is the definition of your URLs since in the version you are using urlpatterns is now a list and the first element '' is invalid. Change it to:

urlpatterns = [
    # Vista para pagina de inicio
    url(r'^$', app.views.index),
    # Otras vistas
    url(r'^admin/', include(admin.site.urls)),
]
    
answered by 15.12.2015 / 20:55
source