简体   繁体   English

没有要重定向到的 URL

[英]No URL to redirect to

I am trying to create a page where these parameters can be filled by the user.我正在尝试创建一个页面,用户可以在其中填写这些参数。 This code allows the data to be stored in mysql but does not show the saved data.此代码允许将数据存储在 mysql 中,但不显示保存的数据。 And shows "ImproperlyConfigured at /public/about/ No URL to redirect to. Either provide a url or define a get_absolute_url method on the Model."并显示“ImproperlyConfigured at /public/about/ 没有要重定向到的 URL。要么提供一个 url,要么在模型上定义一个 get_absolute_url 方法。”

MODELS

class MedicalInfo(models.Model):
    BLOOD = (
        ('A+', 'A+ Type'),
        ('B+', 'B+ Type'),
        ('AB+', 'AB+ Type'),
        ('O+', 'O+ Type'),
        ('A-', 'A- Type'),
        ('B-', 'B- Type'),
        ('AB-', 'AB- Type'),
        ('O-', 'O- Type'),
    )

    @staticmethod
    def toBlood(key):
        for item in MedicalInfo.BLOOD:
            if item[0] == key:
                return item[1]
        return "None"

    patient = models.ForeignKey(User, on_delete=models.CASCADE, related_name="patiento")
    bloodType = models.CharField(max_length=10, choices=BLOOD)
    allergy = models.CharField(max_length=100)
    alzheimer = models.BooleanField()
    asthma = models.BooleanField()
    diabetes = models.BooleanField()
    stroke = models.BooleanField()
    comments = models.CharField(max_length=700)

    def __str__(self):
        return f'{self.user.username} Medinfo'

    def save(self):
        super().save()

VIEWS.PY

class MedinfoCreateView(LoginRequiredMixin, CreateView):
    template_name = 'all_users/public/medinfo.html'
    model = MedicalInfo
    fields = ['bloodType', 'allergy', 'alzheimer', 'asthma', 'diabetes', 'stroke', 'comments']

    def form_valid(self, form):
        form.instance.patient = self.request.user
        return super().form_valid(form)

HTML

{% extends "base.html" %}
{% load crispy_forms_tags %}
{% block content %}
    <div class="content-section">
            <form method="POST">
                {% csrf_token %}
                <fieldset class="form-group">
                    <legend class="border-bottom mb-4">
                        Medinfo
                    </legend>
                    {{ form|crispy }}
                </fieldset>
                <div class="form-group">
                    <button class="btn btn-outline-info" type="submit">
                        Submit
                    </button>
                </div>
            </form>
    </div>
{% endblock content %}

Provide success_url as user will be redirected to it.提供 success_url 因为用户将被重定向到它。 You can also override it by specifying get_success_url() method.您还可以通过指定 get_success_url() 方法来覆盖它。

class GenView(CreateView):
    # Other code
    # If you want simple url
    success_url = "/url/"

    # If you want to redirect dynamically
    get_success_url(self):
        # Some code
        return url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM