Home > Software engineering >  How to solve url parameter problem in Django?
How to solve url parameter problem in Django?

Time:03-27

I have a strange problem.

I have a page that uses url parameters. Users can reach this site based on their project id and name and the group(csoport) name that stored in three models. This is a link for an example page:

/performance/monthlyfeedback/{{ request.user.profile.csoport }}

That link works fine if the name of the csoport is a number. If it is any kind of a text it gives me 404 error that I can't figure out why.

models.py

class Profile(models.Model):
    
    def __str__(self):
        return str(self.user)
    
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    csoport = models.CharField(max_length=100, null=True, blank=True)

class Projekt_perf(models.Model):
    
    def __str__(self):
        return str(self.projekt_perf)
    
    projekt_perf = models.CharField(max_length=250)
    jogosult_01_perf = models.ForeignKey(User, on_delete=models.CASCADE)
    date = models.DateField(auto_now_add=True, auto_now=False, blank=True)

class Performance_profile(models.Model):
    
    def __str__(self):
        return str(self.user_name)

    user_name = models.ForeignKey(User, on_delete=models.CASCADE)
    projekt_perf = models.ForeignKey(Projekt_perf,on_delete=models.CASCADE)

views.py

I am using raw query in the views.

def obj_results(request, projekt_perf_id, projekt_perf, user_name_id, csoport):

    person = Projekt_perf.objects.raw('SELECT * FROM performance_projekt_perf INNER JOIN performance_performance_profile ON performance_projekt_perf.id = performance_performance_profile.projekt_perf_id INNER JOIN stressz_profile ON performance_performance_profile.user_name_id = stressz_profile.user_id WHERE performance_projekt_perf.id = %s AND projekt_perf = %s AND stressz_profile.user_name_id = %s AND stressz_profile.csoport = %s',[projekt_perf_id, projekt_perf, user_name_id, csoport])



    context = {
        'person': person,
    }

    return render(request, 'performance/obj-results.html', context)

urls.py

app_name = 'performance'
urlpatterns = [
    path('monthlyfeedback/<int:projekt_perf_id>', login_required(views.Performance_test), name='performance_feedback'),
    path('list/<int:projekt_perf_id>/<projekt_perf>', login_required(views.list), name='performance_list'),
    path('obj-results/<int:projekt_perf_id>/<projekt_perf>/<int:user_name_id>/<csoport>', login_required(views.obj_results), name='obj_results'),

]

What am I doing wrong?

CodePudding user response:

In urls.py:

path('monthlyfeedback/<int:projekt_perf_id>'

You have defined the “project_perf_id” as int. Change it.

CodePudding user response:

Can you try with <str:csoport> but I reccomend you to use slugs.

path('obj-results/<int:projekt_perf_id>/<projekt_perf>/<int:user_name_id>/<str:csoport>', login_required(views.obj_results), name='obj_results'),
  • Related