I am making a Django project and I want to submit a link name and a link into my form and make it appear on links.html
but when I press submit, it prints onto the console that a post method had happened but when I check the database the data has not saved into it. In case this was a problem with my models I made one through the shell but it seemed to have been fine.
Here is my forms.py:
from django import forms
class LinkForm(forms.Form):
link_text = forms.CharField(label="Link text", max_length=150)
link = forms.URLField(max_length=100000)
models.py:
from django.db import models
# Create your models here.
class Topic(models.Model):
topic_text = models.CharField(max_length=40)
def __str__(self):
return self.topic_text
class Link(models.Model):
topic = models.ForeignKey(Topic, on_delete=models.CASCADE)
link_text = models.CharField(max_length=150)
link = models.URLField(max_length=100000)
def __str__(self):
return self.link_text
links.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Links</title>
</head>
<body>
{% if links %} {% for link in links %}
<a href="{{link.link}}">{{link.link_text}}</a>
<br />
{% endfor %} {% else %}
<h1>This topic has no links</h1>
{% endif %}
<a href="/topics/{{ topic.id }}/newLink">Add a new link</a>
</body>
</html>
newLink.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Create New Link</title>
</head>
<body>
<h1>Create a new link</h1>
<form action="/topics/{{ topic.id }}/" method="post">
{% csrf_token %}
<label for="link_text">Link name: </label>
<input id="link_text" type="text" name-"link_text">
<label for="link">Link: </label>
<input id="link" type="url" name="link" />
<input type="submit" />
</form>
</body>
</html>
views.py:
from django.template import loader
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from .models import Topic, Link
from .forms import TopicForm, LinkForm
# Create your views here.
def newLink(request, id):
topic = get_object_or_404(Topic, pk=id)
links = topic.link_set.all()
return render(request, 'findmylink/newLink.html', {'topic': topic})
def links(request, id):
topic = get_object_or_404(Topic, pk=id)
if request.method == 'POST':
form = LinkForm(request.POST)
if form.is_valid():
l_text = request.POST['link_text']
l_link = request.POST['link']
l = topic.link_set.create(link_text=l_text, link=l_link)
HttpResponseRedirect('/topics/' + id)
else:
form = LinkForm()
links = topic.link_set.all()
return render(request, 'findmylink/links.html', {'topic': topic, 'links': links})
What am I doing wrong here?
Django allows to map a form to a model with the ModelForm class.
https://docs.djangoproject.com/en/3.2/topics/forms/modelforms/
Basically your form should look like:
from django.forms import ModelForm
from path.to.models.module import Link
class LinkForm(ModelForm):
class Meta:
model = Link
fields = ['link_text', 'link']
And your view:
from .forms import LinkForm
def links(request, id):
...
if request.method == 'POST':
form = LinkForm(request.POST)
if form.is_valid():
form.save()
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.