Akhil
Akhil

Reputation: 41

How to send string parameter to url with path?

I want to send a string as parameter to one of my URL in urls.py in Django. The url is path('competitions//about', views.comp_about, name='comp_about'),

Im sending my string through my HTML file as

   <a href="% url 'comp_about' {{c.comp_name}}%">Register Now!</a>

where c is my object with data member comp_name

this is final url after clicking http://127.0.0.1:8000/competitions/%%20url%20'comp_about'%20Comp1%

It says Page not found!

Ive searched SO and google for problem why I am facing.

views.py

@login_required(login_url='login')
def comp_about(request,comp_name):
comps= Competition.objects.get(comp_name=comp_name)
context={
   "comps": comps
}
return render(request,"competition/about.html",context)

html File

<h4>List of Comps</h4>
<br>
<div class="section">
    {% if comps %}
    <ul>   
    {% for c in comps%}
         <li>
            {{c.comp_name}} <br> <a href="% url 'comp_about'       {{c.comp_name}}%">Register Now!</a>
         </li>
         <br><Br>
    {% endfor %}

    {% endif%}
</ul>
</div>

urls.py

path('competitions/<comp_name>/about', views.comp_about, name='comp_about'),

models.py

class Competition(models.Model):
comp_name = models.CharField(max_length=75)
comp_description = models.TextField(max_length=10000)
comp_team_size = models.IntegerField(null=True,blank=False)
comp_payment_fee = models.IntegerField(null=True,blank=True)
comp_rules = models.TextField(max_length=10000,blank=False)
comp_date = models.DateField(null=True,blank=False)
comp_registration_deadline = models.DateField(null=True,blank=False)
class Meta:
    ordering = ['-id']

def __str__(self):
    return self.comp_name 

Upvotes: 0

Views: 811

Answers (1)

2ps
2ps

Reputation: 15926

I think you want something that looks like the following. You can read more about the syntax and what you can do in the django docs. Note that in your OP, you were missing the opening and closing braces { and } needed around a django template filter. Make sure you include them.

<a href="{% url 'comp_about' comp_name=c.comp_name%}">Register Now!</a>

Upvotes: 1

Related Questions