简体   繁体   中英

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. 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."

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. You can also override it by specifying get_success_url() method.

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

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.

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