Я не совсем уверен, как много полей работают в Django.так что у меня есть модель профиля, где в поле имени я хочу добавить имя пользователя, которое я получу из учетных данных для входа, и фильмы будут добавлены из формы, созданной с использованием модели фильма.Я хотел бы понять, что не так в файле views.py здесь.Спасибо введите описание изображения здесь
введите описание изображения здесь
мой файл models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Article(models.Model):
title = models.CharField(max_length = 200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
likes = models.IntegerField(default = 0)
def __str__(self):
return self.title
class Comment(models.Model):
name = models.CharField(max_length = 200)
body = models.TextField()
pub_date = models.DateTimeField('date published')
article = models.ForeignKey(Article, on_delete=models.CASCADE)
class Language(models.Model):
name = models.CharField(max_length = 20)
def __str__(self):
return self.name
class Framework(models.Model):
name = models.CharField(max_length = 20)
language = models.ForeignKey(Language, on_delete=models.CASCADE)
def __str__(self):
return self.name
class Movie(models.Model):
name = models.CharField(max_length = 20)
def __str__(self):
return self.name
class Character(models.Model):
name = models.CharField(max_length = 20)
movies = models.ManyToManyField(Movie)
def __str__(self):
return self.name
class profile(models.Model):
name = models.ForeignKey(User,on_delete=models.CASCADE, related_name="name")
# name = models.CharField(max_length = 20)
movies = models.ManyToManyField(Movie)
def __str__(self):
return self.movies
мои формы.py файл:
from django import forms
from . models import Article, Comment, Movie, Character
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
fields = [
'title',
'body',
'pub_date'
]
class CommentForm(forms.ModelForm):
class Meta:
model = Comment
fields = [
'name',
'body'
]
класс MovieForm (forms.ModelForm):
class Meta:
model = Movie
fields = [
'name'
]
класс CharacterForm (forms.ModelForm):
class Meta:
model = Character
fields = [
'name'
]
мои просмотры.py-файл:
from django.shortcuts import render
from django.contrib.auth.models import User
from .models import Movie, Character, profile
from .forms import MovieForm, CharacterForm
# Create your views here.
def characterset(request):
# form = CharacterForm(request.POST or None)
# if form.is_valid():
# form.save()
# form = CharacterForm()
form2 = MovieForm(request.POST or None)
if form2.is_valid():
form2.save()
form2 = MovieForm()
context = {
'form2' : form2
}
return render(request, "manytomany/form2.html", context)
def movieset(request):
current = request.user.username
print(current)
modelname = User.objects.get(username=current)
print(modelname)
a = profile(name=modelname)
print(a)
form = MovieForm(request.POST or None)
if form.is_valid():
f = form.save()
a.movies.add(f)
a.save()
# form2 = MovieForm(request.POST or None)
# if form2.is_valid():
# form2.save()
# form2 = MovieForm()
context = {
'form' : form
}
return render(request, "manytomany/form.html", context)
def show(request, id):
a = Movie.objects.get(id = id)
c = Character.objects.get(id = id)
c.movies.add(a)
return render(request, "manytomany/show.html")