Skip to main content
título editado
Enlace
César
  • 16.8k
  • 6
  • 42
  • 78

Como ¿Cómo puedo guardar el formulario al usuario logueado?

Como puedo guardar el el formulario al usuario logueado?

mi view

@login_required def agregar(request): current_user = request.user if request.method == 'POST': form = AgregarForm(request.POST, request.FILES) if form.is_valid(): candidate=form.save(commit=False) candidate.user = Producto.objects.get(user=request.user) candidate.save() print candidate return redirect('/cuenta/') else: form = AgregarForm()mi view

@login_required
def agregar(request):
    current_user = request.user 
    if request.method == 'POST':
        form = AgregarForm(request.POST, request.FILES)
        if form.is_valid():
            candidate=form.save(commit=False)
            candidate.user = Producto.objects.get(user=request.user)
            candidate.save()
            print candidate
            return redirect('/cuenta/')
    else:
        form = AgregarForm()

    args = {}
    args['form'] = form
    print args
    return render(request,'users/agregar.html', args)

mi modelo es:

-- coding: utf-8 --

from futuremi modelo es: import unicode_literals

from django.db import models

from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.dispatch import receiver

class Producto(models.Model): modelo = models.CharField(max_length = 100, null=True, blank = True, verbose_name='Ingrese el modelo del Producto') color = models.CharField(max_length = 200, null=True, blank = True, verbose_name='color Producto')

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User    
from django.db.models.signals import post_save
from django.dispatch import receiver


class Producto(models.Model):
    modelo = models.CharField(max_length = 100, null=True, blank = True, 
    verbose_name='Ingrese el modelo del Producto')
    color = models.CharField(max_length = 200, null=True, blank = True, 
    verbose_name='color Producto')

    user = models.ForeignKey(User)
    
    def __unicode__(self):
        return self.modelo

def upload_to(self,filename): ruta = "MultimediaData/Users/%s/%s"%(self.user.username,filename) return ruta

class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) about = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) photo_user = models.ImageField(upload_to=upload_to, blank=True, null=True) photo_team = models.ImageField(upload_to=upload_to, blank=True, null=True)

 

def upload_to(self,filename):
    ruta = "MultimediaData/Users/%s/%s"%(self.user.username,filename)
    return ruta

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    about = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    photo_user = models.ImageField(upload_to=upload_to, blank=True, null=True)
    photo_team = models.ImageField(upload_to=upload_to, blank=True, null=True)

    def __unicode__(self):
        return self.user.username 

Como puedo guardar el el formulario al usuario logueado?

mi view

@login_required def agregar(request): current_user = request.user if request.method == 'POST': form = AgregarForm(request.POST, request.FILES) if form.is_valid(): candidate=form.save(commit=False) candidate.user = Producto.objects.get(user=request.user) candidate.save() print candidate return redirect('/cuenta/') else: form = AgregarForm()

args = {}
args['form'] = form
print args
return render(request,'users/agregar.html', args)

mi modelo es:

-- coding: utf-8 --

from future import unicode_literals

from django.db import models

from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.dispatch import receiver

class Producto(models.Model): modelo = models.CharField(max_length = 100, null=True, blank = True, verbose_name='Ingrese el modelo del Producto') color = models.CharField(max_length = 200, null=True, blank = True, verbose_name='color Producto')

user = models.ForeignKey(User)

def __unicode__(self):
    return self.modelo

def upload_to(self,filename): ruta = "MultimediaData/Users/%s/%s"%(self.user.username,filename) return ruta

class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) about = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) photo_user = models.ImageField(upload_to=upload_to, blank=True, null=True) photo_team = models.ImageField(upload_to=upload_to, blank=True, null=True)

def __unicode__(self):
    return self.user.username 

Como puedo guardar el formulario al usuario logueado?

mi view

@login_required
def agregar(request):
    current_user = request.user 
    if request.method == 'POST':
        form = AgregarForm(request.POST, request.FILES)
        if form.is_valid():
            candidate=form.save(commit=False)
            candidate.user = Producto.objects.get(user=request.user)
            candidate.save()
            print candidate
            return redirect('/cuenta/')
    else:
        form = AgregarForm()

    args = {}
    args['form'] = form
    print args
    return render(request,'users/agregar.html', args)

mi modelo es:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User    
from django.db.models.signals import post_save
from django.dispatch import receiver


class Producto(models.Model):
    modelo = models.CharField(max_length = 100, null=True, blank = True, 
    verbose_name='Ingrese el modelo del Producto')
    color = models.CharField(max_length = 200, null=True, blank = True, 
    verbose_name='color Producto')

    user = models.ForeignKey(User)
    
    def __unicode__(self):
        return self.modelo
 

def upload_to(self,filename):
    ruta = "MultimediaData/Users/%s/%s"%(self.user.username,filename)
    return ruta

class Profile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    about = models.TextField(max_length=500, blank=True)
    location = models.CharField(max_length=30, blank=True)
    birth_date = models.DateField(null=True, blank=True)
    photo_user = models.ImageField(upload_to=upload_to, blank=True, null=True)
    photo_team = models.ImageField(upload_to=upload_to, blank=True, null=True)

    def __unicode__(self):
        return self.user.username 
Origen Enlace

Como puedo guardar el el formulario al usuario logueado?

mi view

@login_required def agregar(request): current_user = request.user if request.method == 'POST': form = AgregarForm(request.POST, request.FILES) if form.is_valid(): candidate=form.save(commit=False) candidate.user = Producto.objects.get(user=request.user) candidate.save() print candidate return redirect('/cuenta/') else: form = AgregarForm()

args = {}
args['form'] = form
print args
return render(request,'users/agregar.html', args)

mi modelo es:

-- coding: utf-8 --

from future import unicode_literals

from django.db import models

from django.contrib.auth.models import User

from django.db.models.signals import post_save

from django.dispatch import receiver

class Producto(models.Model): modelo = models.CharField(max_length = 100, null=True, blank = True, verbose_name='Ingrese el modelo del Producto') color = models.CharField(max_length = 200, null=True, blank = True, verbose_name='color Producto')

user = models.ForeignKey(User)

def __unicode__(self):
    return self.modelo

def upload_to(self,filename): ruta = "MultimediaData/Users/%s/%s"%(self.user.username,filename) return ruta

class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) about = models.TextField(max_length=500, blank=True) location = models.CharField(max_length=30, blank=True) birth_date = models.DateField(null=True, blank=True) photo_user = models.ImageField(upload_to=upload_to, blank=True, null=True) photo_team = models.ImageField(upload_to=upload_to, blank=True, null=True)

def __unicode__(self):
    return self.user.username