Adding a home page with django

0

I have my application in django and I want to add a new page called homepage.html so in my project called web in the views.py I added:

def homepage(request):
        return render_to_response('homepage.html',
        context_instance=RequestContext(request))

Then in web / urls.py I added my url

url(r'^web.views.homepage/', name="homepage"),

Now in my application called crud I have my templates directory in the other folder called crud (so I saw a tutorial that said it was better) and in this directory my homepage.html which I did not put anything just an example text for now

{% extends "base.html" %}

{% block title %} Bienvenido {% endblock %}

{% block content %}
    <h3> Bienvenido , esta es una pagina de inicio </h3>
{% endblock %}

The thing is that when it comes to lifting the server it says

url(r'^web.views.homepage/', name="homepage"),
TypeError: url() missing 1 required positional argument: 'view'

Did I miss something?

    
asked by Naoto 15.11.2017 в 18:52
source

2 answers

1

In your URL you need to define the view that consumes it:

url(r'^web.views.homepage/', [tu_vista.tu_funcion], name="homepage"),

which in your case would be:

url(r'^web.views.homepage/', views.homepage, name="homepage"),

Greetings.

    
answered by 15.11.2017 / 19:10
source
1

I recommend putting it in the urls.py file of your project:

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

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include('<nombre_del_app>.urls', namespace='<nombre_del_app>')),
]

then in your app (crud) create a file urls.py with the following:

from django.conf.urls import url
from . import views

urlpatterns = [
    url(r'^$',views.homepage, name = 'home'),
    # El resto de tus urls con sus respectivas vistas
    # ...
]

And in your views.py I recommend you have something like this:

from django.http import HttpResponse
from django.template import loader
from django.shortcuts import render

def homepage(request):
    # El template debe estar en templates/<nombre_del_app>/<nombre_del_template>.html
    # El template base debe estar en templates/base.html
    template = loader.get_template('<nombre_del_app(crud en este caso)>/homepage.html')
    # El contexto es lo que se va a enviar al template
    # puedes enviar variables y usarlas en el template
    # por ejemplo puedes enviar un Modelo
    context = {}
    return HttpResponse(template.render(context, request))

I hope it helps you.

    
answered by 16.11.2017 в 07:00