输入的字段未保存到模型

时间:2019-03-12 15:32:53

标签: django

我正在尝试在Django中创建一个pastebin克隆。我创建了一个粘贴应用,并在models.py

中创建了一个Paste类
class Paste(models.Model):
    SYNTAX_CHOICES = {
        (0, "Plain"),
        (1, "Python"),
        (2, "HTML"),
        (3, "SQL"),
        (4, "Javascript"),
        (5, "CSS"),
    }

    content     = models.TextField()
    title       = models.CharField(blank=True, max_length=30)
    syntax      = models.IntegerField(choices=SYNTAX_CHOICES, default=0)
    poster      = models.CharField(blank=True, max_length=30)
    timestamp   = models.DateTimeField(default=datetime.datetime.now, blank=True)

    class Meta:
        ordering = ('-timestamp',)

    def __unicode__(self):
        return "%s (%s)" % (self.title or "#%s" % self.id,
            self.get_syntax_display())
    @permalink
    def get_absolute_url(self):
        return ('django.views.generic.list_detail.object_detail',
            None, {'object_id': self.id})

class PasteAdmin(admin.ModelAdmin):
    list_display = ('__unicode__', 'title', 'poster', 'syntax', 'timestamp')
    list_filter = ('timestamp', 'syntax')

admin.site.register(Paste, PasteAdmin)

在我的一个模板中,我有一个表单,要求用户输入所需的详细信息(粘贴名称,语法,粘贴自身)。

{% extends "base.html" %}

{% block content %}
<h1>Your user is</h1>
{{ request.user }}
{{ request.user.is.authenticated }}
<form action="" method="POST">
  {% csrf_token %}
  Title:   <textarea rows="1" cols="50" placeholder="Title of paste"></textarea><br>
  Syntax:   <textarea rows="1" cols="50" placeholder="Enter syntax"></textarea><br>
  {{ form.content }}<br>
  <textarea rows="4" cols="50" placeholder="Please enter the text you'd wish to paste..."></textarea>
  <input type="submit" name="submit" value="Paste" id="submit">
{% endblock content %}

但是,当输入信息时,在检查管理员时不会保存数据。将输入的信息保存到该特定模型时会丢失什么?

我还有一个创建新用户的注册表格,以及一个允许您登录/注销的表格。

pastes/views.py

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            raw_password = form.cleaned_data.get('password1')
            user = authenticate(username=username, password=raw_password)
            login(request, user)
            return redirect('/home')
    else:
        form = UserCreationForm()
    return render(request, 'signup.html', {'form': form})

如何将粘贴链接到用户?还是允许它保持匿名?

对所有(愚蠢的)问题都感到抱歉,我对此非常迷惑,我只使用Django 24小时。

0 个答案:

没有答案