Hi, I have a model that looks like this for reporting users:
class Report(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
is_open = models.BooleanField(default=True)
message = models.TextField(max_length=256)
I specify the view in urls.py with the following:
path('<str:username>/', views.profile, name='profile'),
path('<str:username>/report/', views.ReportView.as_view(), name='report'),
And create the ReportView like this:
class ReportView(CreateView):
model = Report
fields = ["message"]
I link to this using {% url ‘report’ user.username %} from a template, which gives me the error:
NoReverseMatch at /jim/report/
Reverse for 'report' with no arguments not found. 1 pattern(s) tried: ['(?P<username>[^/]+)/report/$']
I don’t understand where the “no arguments” is coming from here. I know how to pass the user with self.request.kwargs[‘username’] to the view, but I don’t understand where arguments aren’t being supplied. As above, I am specifying the username needed in my URL tag.
Any ideas?
Data for rendering templates is supplied in the context being passed to the render function.
When data isn’t supplied, the template language renders it as a blank - which in the case of the url tag makes it look like no parameter was supplied to it.
If that’s the complete view, then you need to override the get_context_data method on that class to add the user to it.
Thanks, I’ve implemented get_context_data as follows:
class ReportView(CreateView):
model = Report
fields = ["message"]
def get_context_data(self, **kwargs):
ctx = super(ReportView, self).get_context_data(**kwargs)
ctx['username'] = self.kwargs['username']
return ctx
and still get the same error:
Reverse for 'report' with no arguments not found. 1 pattern(s) tried: ['(?P<username>[^/]+)/report/$']
Your template is looking for the username attribute of an object in the context named “user” …
… but you’re putting the username directly in the context, not user.
I’m not sure I understand.
On the page I’m linking from {{ user.username }} is defined by that page’s context.
The ReportView is a different page, and I believe self.kwards[‘username’] is being taken from the pattern in urls.py?
Forget anything related to URLs and links for the moment.
You have a template for a page. Any page, it doesn’t matter what it is.
Let’s say that in that template, you have a field {{ user.username }}
This means that the render function for that page is going to look for a user object in the context, and retrieve the username attribute from it.
However, in the get_context_data method in the view that is generating this page, you’re not putting the user object in the context. You’re putting username.
I see, so then I want something like:
def get_context_data(self, **kwargs):
ctx = super(ReportView, self).get_context_data(**kwargs)
ctx['user'] = get_object_or_404(User, username=self.kwargs['username'])
return ctx
this still gives me the same error.
First, since there seems to be some confusion, I want to make sure we’re talking about the right view and template here, so I want to take a step back and make sure we’re talking about the same things.
You have a view to create a page.
This page is supposed to have a link to a different page named “report”, that takes the username as a parameter. (Note: What this “target” page is is totally irrelevant. It just doesn’t matter for this discussion.) However, you’re getting an error on the page that is supposed to generate this link.
What can we call this page?
я, не буду связывать ваш код, просто у меня (что то подлобное-информация к размышлению)
city = get_object_or_404(City, name_en=kwargs[“city_en”])
redirect_to = reverse(“city_ordering_list”, kwargs={“city_en”: city})
…
def get_success_url(self):
city = get_object_or_404(City, name_en=self.kwargs[“city_en”])
return reverse(“city_ordering_list”, kwargs={“city_en”: city})
…
extra_context[“city_en”] = city
templates
{% url ‘core:service-detail’ city_en=view.kwargs.city_en slug=item.slug %}
city list services | {{ city_en }}
I have the “profile page” with a link to the “report page” (the CreateView).
The error is not on the page with the link.
The profile page renders fine, the error shows when I click it.
If you’re going to a URL and getting an error with rendering the page, the problem is in that target page. How you got there is irrelevant.
So if the “report page” is the problem, please post all the relevant components for this page - the complete view, form, and template, but only for this “report page”.
I did earlier, the report page is just
class ReportView(CreateView):
model = Report
fields = ["message"]
def get_context_data(self, **kwargs):
ctx = super(ReportView, self).get_context_data(**kwargs)
ctx['username'] = self.kwargs['username']
return ctx
There is no template yet and the form is generated by the generic view
Ok, let’s try this from a different angle - can you post the complete traceback from your runserver console where the error is appearing?
I just found the problem, sorry. There is a template with a broken url tag I completely forgot about. I thought the problem was that the class couldn’t read username from the URL or something.