Dostaję błąd Nazwa: Nazwa "Sprzedam" nie jest zdefiniowana, ale zdefiniowałem ten model w modelach.py plz, powiedzmy, jaki jest błąd, który opublikowałem mój odpowiedni kod. Z góry dziękuję. Modele.py zakupu aplikacji

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 of Kup App

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)

Formy.py aplikacji

    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')

Sprzedaj.html.

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

Nie używałem Django UserCreateForm stworzyłem go w ten sposób w innej aplikacji o nazwie Konta: Views.py aplikacji

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 lipiec 2020, 16:34

1 odpowiedź

Najlepsza odpowiedź

Wystarczy zaimportować model

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 lipiec 2020, 13:40