-1

I have tried following this suggestion to pass string parameters to a class based view but it does not seem to work.

the url:

url(r'^chart/(?P<chart_name>\w+)/$',
        ChartView.as_view(chart_name='chart_name'), name="chart_url"),

the view:

class ChartView(View):
    template_name = "chart.html"
    chart_name = None

    def post(self, request, *args, **kwargs):
        form = DatesForm(request.POST)
        context = {
            'form': form
        }
        return render(request, self.template_name, context)

    def get(self, request, *args, **kwargs):
        print("test")
        form = DatesForm()
        # fetch plot data (default values used)
        context = {
            'form': form,
            'chart_name': self.chart_name
        }
        return render(request, self.template_name, context)

the link that is supposed to be redirecting to the view:

<a href="{% url 'chartboard:chart_url' chart_name='lords' %}">Sometext</a>

(namespace 'chartboard' given in the project's urlconf).

the error:

NoReverseMatch at /chart/lords/
Reverse for 'chart_url' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: ['chart/(?P<chart_name>\\w+)/$']

For what its worth, "test" gets printed twice to the console output (why?)

Using django 1.8.11 and python 3.4.3 on Ubuntu 14.04.04

4
  • 1
    Does the traceback show the line that is causing the error? I don't think it's the link you posted, because the chart_name='lords' doesn't match the error message. Commented Apr 7, 2016 at 14:42
  • @Alasdair yep you are right! I am using this url without parameters in another place of the template and hence the specific error Commented Apr 7, 2016 at 14:49
  • Have you managed to fix the problem? If not, post the line that is failing. Commented Apr 7, 2016 at 15:03
  • 1
    It's OK now. This is the line that was failing: <form action="{% url 'chartboard:chart_url' %}" method="post" class="form-inline">{% csrf_token %}. It has now been replaced by this: <form action="{% url 'chartboard:chart_url' chart_name=chart_name %}" method="post" class="form-inline">{% csrf_token %} Commented Apr 7, 2016 at 17:36

1 Answer 1

1

You should access the chart_name using kwargs:

# urls.py
url(r'^chart/(?P<chart_name>\w+)/$',
    ChartView.as_view(), name="chart_url"),

# and then in the view

class ChartView(View):
    template_name = "chart.html"

    def get(self, request, *args, **kwargs):
        form = DatesForm()
        context = {
            'form': form,
            'chart_name': kwargs['chart_name'] # here you access the chart_name
        }
        return render(request, self.template_name, context)

The post you have considered for implementing this is for making sure that a variable is available in templates and that is taken care of by setting it up in context which is passed to the template render.

The problem you are facing here is to access a named group defined in the url pattern.

Here is more documentation on how django process a request when you try to access a URL.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.