October 25, 2024
Chicago 12, Melborne City, USA
python

Django Under the Impression that I'm Missiing Positional Arguements


So Django is currently under the impression that I am missing positional arguments when I’m putting together a help section for a website. This includes help articles which are sourced from the Django model.

Everything works as expected until I include the unique help article reference number into my views.py file.

Django believes that positional arguments are missing when this is simply not true. As you will see in my MRE below, there are no missing positional arguments.

The error message is as follows:

help_center_article_index() missing 1 required positional argument: 'support_article_reference'

MRE:

#utils.py
import requests, random, string
def generate_unique_number(charlimit):

    random_string = ''.join(random.choices(string.digits, k=charlimit))  # Generates a random string
    return f"{random_string}"
#models.py
from .import utils
class SupportArticles(models.Model):
    class TargetGroup(models.TextChoices):
        FOR_DEVELOPERS = 'developers','Developers'
        FOR_Clients="clients",'Clients'
        FOR_FX_Providers="fx-providers",'FX Providers'

    support_article_reference       =   models.CharField(max_length=50, default=utils.generate_unique_number(15), editable=False)
    support_article_title           =   models.CharField(max_length=255, verbose_name="Support Article Title")
    support_article_body            =   models.TextField(max_length=5000,verbose_name="Support Article Body")
    support_article_tags            =   models.CharField(max_length=100,default="Separate each tag category with a comma (,)", verbose_name="Support Topic Tags")
    support_article_group           =   models.CharField(max_length=15,choices=TargetGroup.choices,default=TargetGroup.FOR_Clients)
#views.py
from django.shortcuts import render, get_object_or_404
def help_center_article_index(request, group, support_article_reference):
   
   support_article_object     = SupportArticles.objects.filter(support_article_group = group, support_article_reference = support_article_reference)
   if support_article_object.exists():
      meta_property_title     = support_article_object.first().support_article_group)
   else:
      meta_property_title     = "No articles available for this group"

   context = {
      
      'meta_property_title'       : meta_property_title,
      'meta_property_description' : meta_property_description,
      'support_article_object'    : support_article_object,
      'group'                     : group,

  }
   
   return render (request, 'help_center_article_index.html', context
#urls.py
from django.urls import path
from django.shortcuts import render
from . import views

urlpatterns = [
    path('hc',views.help_center, name="help-center"),
    path('hc/<str:group>',views.help_center_article_index, name="help-center-article"),
    path('hc/<str:group>/<str:support_article_reference>',views.help_center_article, name="support_article")
]
#help_center_article_index.html
<div class="support-article-index__container">
        {% for support_article in support_article_object %}
        <a href="/hc/{{group}}/{{support_article_object.support_article_reference}}" class="support-article-index__inserted">
            <h3>{{ support_article.support_article_title | title}}</h3>
            <p>{{support_article.support_article_body}}</p>
        </a>
        {% empty %}
        <h3 style="text-align: center;">{{meta_property_title}}</h3>
        {% endfor %}
    </div>

I fail to see why Django returns this error when all positional arguments are present and correct. How would I essentially tell Django that this error message should not rear its ugly head?

Tried reading a few articles, but all here no good.

I was expecting to click on the url link, which will take me to the relavent article with the url being:

hc/<group>/<support_article_reference>

i.e.

hc/developers/123456789123



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video