S
S
Stepan Yadryshnov2020-03-10 10:53:11
Django
Stepan Yadryshnov, 2020-03-10 10:53:11

django NoReverseMatch error?

I decided to master django, I thought that it would be enough to repeat everything after the man from the video, I need to try something myself without prompts and I ran into this.

Reverse for 'boss' with no arguments not found. 1 pattern(s) tried: ['mymeteo/gorod/(?P[0-9]+)/$']
Request Method: GET
Request URL: 127.0.0.1:8000/mymeteo/gorod/4
Django Version: 3.0. 3
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'boss' with no arguments not found. 1 pattern(s) tried: ['mymeteo/gorod/(?P[0-9]+)/$']
Exception Location: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site -packages/django/urls/resolvers.py in _reverse_with_prefix, line 677
Python Executable: /Library/Frameworks/Python.framework/Versions/3.
Python Version: 3.8.1
Python Path:
['/Users/stepan/Desktop/django-pyowm/meteo',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python38.zip',
'/Library/ Frameworks/Python.framework/Versions/3.8/lib/python3.8',
'/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/lib-dynload',
'/Library/Frameworks/Python. framework/Versions/3.8/lib/python3.8/site-packages'] Server time: Tue, 10 Mar 2020
07:30:43

+0000

.py

from django.urls import path, include
from . import views

urlpatterns = [
    path ('list/', views.ListGorod.as_view(), name = 'listgorod'),
    path ('gorod/<int:id>/', views.MeteoList.as_view(), name = 'boss'),
]


this is what views.py looks like

class MeteoList(View):

  def get (self, request, id):
    obj = Gorod.objects.get ( id__iexact = id)

    form = GorodForm(instance=obj)

    try:

      o=pyowm.OWM("262fea7ee569218476043b47466017ea")
      obs=o.weather_at_place(obj.gorod)
      w=obs.get_weather()
    except:
      o=pyowm.OWM("262fea7ee569218476043b47466017ea")
      obs=o.weather_at_place('biysk, RUS')
      w=obs.get_weather()

    wind = w.get_wind()
    temperature = w.get_temperature('celsius')
    return render(request, 'mymeteo/meteo_list.html',  context ={'temp' : w, 'temperature' : temperature, 'form':form})

  def post (self, request, id):
    obj = Gorod.objects.get (id__iexact = id)
    b_form = GorodForm(request.POST, instance=obj)

    new_obg = b_form.save()



    try:

      o=pyowm.OWM("262fea7ee569218476043b47466017ea")
      obs=o.weather_at_place(obj.gorod)
      w=obs.get_weather()
    except:
      o=pyowm.OWM("262fea7ee569218476043b47466017ea")
      obs=o.weather_at_place('biysk, RUS')
      w=obs.get_weather()

      wind = w.get_wind()
      temperature = w.get_temperature('celsius')
      return render(request, 'mymeteo/meteo_list.html',  context ={'temp' : w, 'temperature' : temperature, 'form':form})
    return redirect(new_obg)


and this is how get_absolute_url looks like in models

class Gorod (models.Model):
  gorod = models.CharField(max_length = 50)

  def get_absolute_url(self):
    return reverse("boss", kwargs={"id" : self.id})

  


  def  __str__ (self):
    return self.gorod


I understand that most likely here, in addition to, there are a bunch of other errors .. but I can’t even get to them .. because I encounter NoReverseMatch. I started this for the sake of learning and practice.
At the same time, this is not the first time I have been trying to make some kind of test projects not from video, but on my own. And up to this point, such designs have worked.
In general, I have a direct total not understanding why this error is triggered.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stepan Yadryshnov, 2020-03-10
@stepan2112

problem solved. it turned out to be very stupid of course .. but if it suddenly comes in handy ...
then in the template of this page in the form tag in actions there was the wrong address
thanks to everyone who tried to help)
ps in the end everything works and in get_absolute_url I still have kwargs

A
alternativshik, 2020-03-10
@alternativshik

the text of the error shows what janga will turn your urls into. And there is not a single kwarg in that url, so you need to write reverse("boss", args=[self.id])

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question