Recibo el error NameError: el nombre 'Sell' no está definido. Pero he definido este modelo en Models.py Por favor, diga cuál es el error. He publicado mi código relevante. Gracias por adelantado. Models.py de la aplicación de compra

from django.db import models
from django.contrib.auth.models import User


# Create your models here.

class Sell(models.Model):
    Cats =( 
    ('Course book','Course book'), 
    ('Fiction','Fiction'), 
    )
    Lang =(
    ('Hindi','Hindi'),
    ('English','English'),
    ('Tamil','Tamil')
    )
    Cond = (
    ('New','New'),
    ('Old','old')
    )
    Title = models.CharField(max_length=400)
    Author = models.CharField(max_length=100)
    Price = models.DecimalField()
    Category = models.CharField(choices=Cats)
    Language = models.CharField(choices=Lang)
    Condition = models.CharField(choices=Cond)
    user = models.ForeignKey(User, on_delete=models.CASCADE)

Views.py de la aplicación de compra

class sell(TemplateView):
template_name = 'buy/sell.html'

def get(self,request):
    form = sellForm()
    return render(request,self.template_name,{'form':form})

def post(self,request):
    text = None
    form = sellForm(request.POST)
    if form.is_valid():
        print("all good")
        text = form.cleaned_data['Title']
        form = sellForm()

    args = {'form':form,'text':text}
    return render(request,self.template_name,args)

Aplicación Forms.py de cuentas

    from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm

class EditProfileForm(UserChangeForm):
    class Meta:
        model = User
        fields =(
            'first_name',
            'last_name',
            'email',
            'password'
        )

class sellForm(forms.ModelForm):
    Cats =( 
    ('Course book','Course book'), 
    ('Fiction','Fiction'), 
    )
    Lang =(
    ('Hindi','Hindi'),
    ('English','English'),
    ('Tamil','Tamil')
    )
    Cond = (
    ('New','New'),
    ('Old','old')
    )
    Title = forms.CharField()
    Author = forms.CharField()
    Price = forms.DecimalField()
    Category = forms.ChoiceField(required=False,choices=Cats)
    Language = forms.ChoiceField(choices=Lang)
    Condition = forms.ChoiceField(choices=Cond)
    Description = forms.CharField()
class Meta:
    model = Sell
    fields = ('Title','Author','Price','Category','Language','Condition','Description')

Sell.html

<form method="POST">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>

No usé Django UserCreateForm, lo creé de esta manera en otra aplicación llamada cuentas: Views.py of accounts app

def register(request):

if request.method =='POST':
    first_name = request.POST['first_name']
    last_name= request.POST['last_name']
    username= request.POST['username']
    password1 = request.POST['password1']
    password2 = request.POST['password2']
    email = request.POST['email']

    if password1 == password2:
        if User.objects.filter(username=username).exists():
            messages.info(request,'Username taken')
            return redirect('register')
        elif User.objects.filter(email=email).exists():
            messages.info(request,'email exists')
            return redirect('register')
        else:
            user = User.objects.create_user(username=username,password=password1,email=email,first_name=first_name,last_name=last_name)
            user.save()
            messages.info(request,'user created')
            return redirect('login')
    else:
        messages.info(request,'password not matching')
        return redirect('register')
    return redirect('/')
else:
    return render(request,'buy/register.html')
0
Tanvi101 26 jul. 2020 a las 16:34

1 respuesta

La mejor respuesta

Solo necesitas importar el modelo

from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserChangeForm

from .models import Sell
1
Harben 26 jul. 2020 a las 13:40