trouselife
trouselife

Reputation: 969

Django: Change url in Class based view GET request

Hi I have two forms which render to the same template.

When i submit my runsearchform, it takes me to the page:

 localhost/reporting/?run=2&submit=Search+for+run

where 2 is my run PK id

How can I modify this URL into something like

 localhost/reporting/run_name

where run_name = a unique field in my Run model that is not the primary key

views:

class ReportView(View):

    runsearchform = RunSearchForm
    samplesearchform = SampleSearchForm

    def get(self, request, *args, **kwargs):

        runsearchform = self.runsearchform()
        context = {'runsearchform': runsearchform}

        if 'run' in request.GET:
            samplesearchform = self.samplesearchform(request.GET)
            context = {'samplesearchform': samplesearchform}


        return render(request, 'results/reporting.html', context)



    def post(self, request, *args, **kwargs):

        """ do stuff with samples...
        """

Upvotes: 0

Views: 537

Answers (1)

Will Keeling
Will Keeling

Reputation: 23014

You could specify the run_name in your urls.py such as:

url(r'^(?i)reporting/(?P<run_name>[a-z0-9]+)/$', myapp.views.ReportView.as_view())

And then your view could receive the parameter such as:

class ReportView(View):

    runsearchform = RunSearchForm
    samplesearchform = SampleSearchForm

    def get(self, request, run_name):
        # Load the report based on the run_name, and do something with it

When a request is made to e.g. localhost/reporting/myreport, the run_name argument would be set to myreport.

Upvotes: 1

Related Questions