Answer the question
In order to leave comments, you need to log in
The form is saved, it is visible in the admin panel, but it is not displayed on the site. How to solve the problem?
Hello everyone, the problem, in fact, described in the header. I created two fields for adding a note, the note is created successfully (it is displayed in the admin panel). But it is not on the site itself, it is not displayed.
It seems to me that the problem is in the html form, but I will ask for the opinions of experts.
Here is the code.
View.
def home(request):
notes = Note.objects
template = loader.get_template('MainPage/note.html')
form = NoteForm(request.POST or None)
if form.is_valid():
notes = form.save(commit=False)
notes.author = request.user
notes.save()
else:
form = NoteForm()
context = {'notes': notes, 'form': form}
return render(request, 'MainPage/note.html', context)
!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Note</title>
<link href="http://codepen.io/edbond88/pen/CcgvA.css" media="screen" rel="stylesheet" type="text/css">
<style>
body {<br />
background: rgba(222,222,222,1);<br />
margin: 20px;<br />
}<br />
</style>
</head>
<h1>Django Note Taking App</h1>
<body>
<form method="POST" action="">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Добавить заметку">
</form>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
The code shows that you don’t understand anything at all, I don’t know how to fix it. Alternatively, go through the tutorial again on the official site or from django-girls.
1. What does it mean that the form is not visible, what do you expect to see?
2. Why do you need template = loader.get_template('MainPage/note.html')?
3. Why are you overriding notes with one saved note?
4. Where is the output of notes in the template?
You pass notes to the template, implying that there is a list of notes. Why don't you use notes in the template? So that they actually come out.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question