python – Django Cannot Save the Form-ThrowExceptions

Exception or error:

I want to create a patient form for my system. I entry all values but it cannot save the patient and shows an error. Where is my mistake?

FieldError at /create/ Cannot resolve keyword ‘slug’ into field.
Choices are: address, created_date, dept, first_name, id, last_name,
notes, phone, title

Thanks for your attention.

Traceback:

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\core\handlers\base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\edeni\senior\pharsys\patients\views.py" in patient_create
  29.         post.save()

File "C:\Users\edeni\senior\pharsys\patients\models.py" in save
  39.         self.slug = self.get_unique_slug()

File "C:\Users\edeni\senior\pharsys\patients\models.py" in get_unique_slug
  33.         while newPatients.objects.filter(slug=unique_slug).exists():

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\manager.py" in manager_method
  82.                 return getattr(self.get_queryset(), name)(*args, **kwargs)

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\query.py" in filter
  892.         return self._filter_or_exclude(False, *args, **kwargs)

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\query.py" in _filter_or_exclude
  910.             clone.query.add_q(Q(*args, **kwargs))

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\sql\query.py" in add_q
  1290.         clause, _ = self._add_q(q_object, self.used_aliases)

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\sql\query.py" in _add_q
  1318.                     split_subq=split_subq, simple_col=simple_col,

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\sql\query.py" in build_filter
  1190.         lookups, parts, reffed_expression = self.solve_lookup_type(arg)

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\sql\query.py" in solve_lookup_type
  1049.         _, field, _, lookup_parts = self.names_to_path(lookup_splitted, self.get_meta())

File "C:\Users\edeni\senior\myenv\lib\site-packages\django\db\models\sql\query.py" in names_to_path
  1420.                                      "Choices are: %s" % (name, ", ".join(available)))

Exception Type: FieldError at /create/
Exception Value: Cannot resolve keyword 'slug' into field. Choices are: address, created_date, dept, first_name, id, last_name, notes, phone, title

views.py

def patient_create(request):

    if not request.user.is_authenticated:
        return render(request, "http404.html")

    form = PatientForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        post = form.save(commit=False)
        post.user = request.user
        post.save()
        messages.success(request, "Başarılı bir şekilde oluşturdunuz.", extra_tags='mesaj-basarili')
        return HttpResponseRedirect(post.get_absolute_url())

    context = {
        'form': form
    }

    return render(request, "patient_form.html", context)

models.py

class newPatients(models.Model):

    title = models.CharField(max_length=100)
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    created_date = models.DateTimeField(default=timezone.now)
    dept = models.TextField()
    address = models.TextField()
    phone = models.CharField(max_length=15)
    notes = RichTextField(verbose_name="notes")

    def publish(self):
        self.published_date = timezone.now()
        self.save()

    def __str__(self):
        return self.title

    def get_create_url(self):
        return reverse('post:create', kwargs={'slug': self.slug})

    def get_unique_slug(self):
        slug = slugify(self.title.replace('ı', 'i'))
        unique_slug = slug
        counter = 1
        while newPatients.objects.filter(slug=unique_slug).exists():
            unique_slug = '{}-{}'.format(slug, counter)
            counter += 1
        return unique_slug

    def save(self, *args, **kwargs):
        self.slug = self.get_unique_slug()
        return super(newPatients, self).save(*args, **kwargs)

    class Meta:
        ordering = ['-created_date']

patient_form.html

...
{% if user.is_authenticated %}

  <div id="form-main">
  <div id="form-div">
    <form class="form" id="form1">

      <p class="name">
        <input name="title" type="text" required id="id_title"class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Patient Title" />
      </p>

      <p class="name">
        <input name="first_name" type="text" required id="id_first_name"class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="First Name" />
      </p>

      <p class="name">
        <input name="last_name" type="text" required id="id_last_name"class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Last Name"  />
      </p>

      <p class="name">
        <textarea name="dept" cols="40" required rows="10" placeholder="Dept" class="validate[required,length[6,300]] feedback-input"  required id="id_dept"></textarea></p>
      </p>


      <p class="text">
        <textarea name="address"  required class="validate[required,length[6,300]] feedback-input" id="id_address" placeholder="Address"></textarea>
      </p>
      <p class="name">
        <input name="phone" maxlength="15" required type="text"  id="id_phone"class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Phone"  />
      </p>

      <p><div  class="django-ckeditor-widget" data-field-id="id_notes" style="display: inline-block;">
    <textarea placeholder="Notes" cols="500" id="id_notes" name="notes" rows="10" required data-processed="0" data-config="{&quot;skin&quot;: &quot;moono-lisa&quot;, &quot;toolbar_Basic&quot;: [[&quot;Source&quot;, &quot;-&quot;, &quot;Bold&quot;, &quot;Italic&quot;]], &quot;toolbar_Full&quot;: [[&quot;Styles&quot;, &quot;Format&quot;, &quot;Bold&quot;, &quot;Italic&quot;, &quot;Underline&quot;, &quot;Strike&quot;, &quot;SpellChecker&quot;, &quot;Undo&quot;, &quot;Redo&quot;], [&quot;Link&quot;, &quot;Unlink&quot;, &quot;Anchor&quot;], [&quot;Image&quot;, &quot;Flash&quot;, &quot;Table&quot;, &quot;HorizontalRule&quot;], [&quot;TextColor&quot;, &quot;BGColor&quot;], [&quot;Smiley&quot;, &quot;SpecialChar&quot;], [&quot;Source&quot;]], &quot;toolbar&quot;: &quot;Full&quot;, &quot;height&quot;: 291, &quot;width&quot;: 835, &quot;filebrowserWindowWidth&quot;: 940, &quot;filebrowserWindowHeight&quot;: 725, &quot;language&quot;: &quot;en-us&quot;}" data-external-plugin-resources="[]" data-id="id_notes" data-type="ckeditortype"></textarea>
</div></p>


      <div class="submit">
        <button type="submit" id="button-blue">SAVE</button>
        <div class="ease"></div>
      </div>
    </form>
  </div>

{% endif %}

...
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *