Alfredhb.q
Alfredhb.q

Reputation: 75

how call severals functions in a just View

Well, i am new using Python and Django then, i wish to know how i can call severals functions in a view, for example i am doing a app where the people buy server and also recive notifications, then i want call the functions to work all of this without problem, i do Log in Works but sign up no, and notifications Works when i disable the form call to make registration, because i want all this things in the same page, for example call this in Layout.html, is like base, now looks my views

Views.py

def index(request):
    if request.method == 'POST':
        form = RegistroUserForm(request.POST, request.FILES)
    else:
        form = RegistroUserForm()
    context = {
        'form': form
    }
    notifi = Notificaciones.objects.all()
    return render(request,'app/index.html',context)

def registro_usuario_view(request):
    if request.method == 'POST':
        # Si el method es post, obtenemos los datos del formulario
        form = RegistroUserForm(request.POST, request.FILES)

        # Comprobamos si el formulario es valido
        if form.is_valid():
            # En caso de ser valido, obtenemos los datos del formulario.
            # form.cleaned_data obtiene los datos limpios y los pone en un
            # diccionario con pares clave/valor, donde clave es el nombre del campo
            # del formulario y el valor es el valor si existe.
            cleaned_data = form.cleaned_data
            username = cleaned_data.get('username')
            password = cleaned_data.get('password')
            email = cleaned_data.get('email')
            # E instanciamos un objeto User, con el username y password
            user_model = User.objects.create_user(username=username, password=password)
            # Añadimos el email
            user_model.email = email
            # Y guardamos el objeto, esto guardara los datos en la db.
            user_model.save()
            # Ahora, creamos un objeto UserProfile, aunque no haya incluido
            # una imagen, ya quedara la referencia creada en la db.
            user_profile = UserProfile()
            # Al campo user le asignamos el objeto user_model
            user_profile.user = user_model
            # Por ultimo, guardamos tambien el objeto UserProfile
            user_profile.save()
    else:
        # Si el mthod es GET, instanciamos un objeto RegistroUserForm vacio
        form = RegistroUserForm()
    # Creamos el contexto
    context = {'form': form}
    # Y mostramos los datos
    return render(request, 'app/registro.html', context)

then in my Layout.html, i include registro.html and notificaciones.html, but in the view only can call registro or notificaciones, i wish know how i include both and both Works.

Upvotes: 2

Views: 60

Answers (1)

xbello
xbello

Reputation: 7443

I think you have some wrong concepts. The templates (Layout.html) doesn't call any functions. The code in views.py uses the templates to create a full HTML page that is going to be handled to the browser

Your two functions share a lot of code, except for this line in the first one all remains the same:

notifi = Notificaciones.objects.all()

You probably want to get rid of def index() and move the previous line to def registro_usuario_view().

If I understood it right, you want to render Notificaciones in the templates. But you are not passing notifi to the renderers anywhere. You have to add it to the context. For example:

notifications = Notifications.objects.all()

context = {"form": form,
           "notifications": notifications}

Then in your templates you have access to {{ notifications }} and its fields and methods, lets say:

{% for notification in notifications %}
    In {{ notifications.date }}, {{ notifications.author }} said:
    {{ notifications.message }}
{% endfor %}

Upvotes: 2

Related Questions