Error after posting new data to the web server
Question:
It all looks good by when i try to add some post to my topic I have an error created_by=user
Here is my forms.py
from django import forms
from .models import Topic
class NewTopicForm(forms.ModelForm):
message = forms.CharField(
widget=forms.Textarea(
attrs={'rows': 5, 'placeholder': 'What is on your mind?'}
),
max_length=40000,
help_text='The max length of the text is 4000.')
class Meta:
model = Topic
fields = ['subject', 'message']
and my view function for this form:
def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk)
user = User.objects.first()
if request.method == 'POST':
form = NewTopicForm(request.POST)
if form.is_valid():
topic = form.save(commit=False)
topic.board = board
topic.starter = user
topic.save()
post = Post.objects.create(
message=form.cleaned_data.get('message'),
topic=topic,
created_by=user
)
return redirect('board_topics', pk=board.pk)
else:
form = NewTopicForm()
return render(request, 'new_topic.html', {'board': board, 'form': form})
models.py
class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, related_name='t_posts', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(Topic, related_name='posts', on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, related_name='+', null=True, on_delete=models.CASCADE)
Answers:
There is a mistake in your Post model
created_by needs to be a Foreign Key to the User table
created_by = models.ForeignKey(User, related_name='created_by', on_delete=models.CASCADE)
run the following commands after the above changes to reflect the changes in the database
python manage.py makemigrations
python manage.py migrate
What’s the best account I can have? A single person is only permitted to have one Coinbase Pro account. To avoid breaking this rule, if you already have a Coinbase account and want to create a Coinbase Pro account, use the same login email address.
https://posteezy.com/uphold-bitcoin
https://justpaste.it/6rwzm
It all looks good by when i try to add some post to my topic I have an error created_by=user
Here is my forms.py
from django import forms
from .models import Topic
class NewTopicForm(forms.ModelForm):
message = forms.CharField(
widget=forms.Textarea(
attrs={'rows': 5, 'placeholder': 'What is on your mind?'}
),
max_length=40000,
help_text='The max length of the text is 4000.')
class Meta:
model = Topic
fields = ['subject', 'message']
and my view function for this form:
def new_topic(request, pk):
board = get_object_or_404(Board, pk=pk)
user = User.objects.first()
if request.method == 'POST':
form = NewTopicForm(request.POST)
if form.is_valid():
topic = form.save(commit=False)
topic.board = board
topic.starter = user
topic.save()
post = Post.objects.create(
message=form.cleaned_data.get('message'),
topic=topic,
created_by=user
)
return redirect('board_topics', pk=board.pk)
else:
form = NewTopicForm()
return render(request, 'new_topic.html', {'board': board, 'form': form})
models.py
class Post(models.Model):
message = models.TextField(max_length=4000)
topic = models.ForeignKey(Topic, related_name='t_posts', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(null=True)
created_by = models.ForeignKey(Topic, related_name='posts', on_delete=models.CASCADE)
updated_by = models.ForeignKey(User, related_name='+', null=True, on_delete=models.CASCADE)
There is a mistake in your Post model
created_by needs to be a Foreign Key to the User table
created_by = models.ForeignKey(User, related_name='created_by', on_delete=models.CASCADE)
run the following commands after the above changes to reflect the changes in the database
python manage.py makemigrations
python manage.py migrate
What’s the best account I can have? A single person is only permitted to have one Coinbase Pro account. To avoid breaking this rule, if you already have a Coinbase account and want to create a Coinbase Pro account, use the same login email address.
https://posteezy.com/uphold-bitcoin
https://justpaste.it/6rwzm