django reverse url erro - python

I have following code in urls.py
urlpatterns=[
path('estimate_insert/',views.estimate_ui,name='estimate_i'),
path('<int:id>/',views.estimate_ui,name='estimate_u'),
path('estimate_delete/<int:id>/',views.estimate_d,name='estimate_d'),
path('estimate_preview/<int:id>/',views.estimate_preview,name='estimate_v'),
]
I am matching these urls to the following code in views.py
def estimate_ui(request,id=0):
if request.method=="GET":
if id==0:
form=EstimateForm()
else:
obj_estimate=Estimate.objects.get(pk=id)
form=EstimateForm(instance=obj_estimate)
return render(request,"Invoices/estimate_ui.html",{'form':form})
else:
if id==0:
form=EstimateForm(request.POST)
else:
obj_estimate=Estimate.objects.get(pk=id)
form=EstimateForm(request.POST,instance=obj_estimate)
if form.is_valid():
form.save()
return redirect('/invoices/')
the below mentioned url is causing problem when I access it.
path('<int:id>/',views.estimate_ui,name='estimate_u'),
When I access I get the following error
NoReverseMatch at /invoices/status/3/
Reverse for 'estimate_u' with arguments '('',)' not found. 1 pattern(s) tried: ['estimate/(?P<id>[0-9]+)/$']
Request Method:
GET
Request URL:
http://127.0.0.1:8000/invoices/status/3/
Django Version:
3.1.4
Exception Type:
NoReverseMatch
Exception Value:
Reverse for 'estimate_u' with arguments '('',)' not found. 1 pattern(s) tried: ['estimate/(?P<id>[0-9]+)/$']
can anyone explain why is this happening. I am accessing index.html--> linked to move to status.html(it has link to move estimate.html)--> at estimate.html I have many steps its basically a workflow based application. I watched almost all url related videos at youtube, searched google but found nothing that could help. thanks

Related

NoReverseMatch at / Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['posts/(?P<pk>[0-9]+)/$']

I'm building a django blog app. But, when I'm trying to build detail pages for blog posts, I'm getting this error:
NoReverseMatch at /
Reverse for 'detail' with arguments '('',)' not found. 1 pattern(s) tried: ['posts/(?P<pk>[0-9]+)/$']
This is my post_detail view function:
def post_detail(request,post_id):
post = get_object_or_404(Post, id=post_id)
return render(request, 'blog/detail.html',{'post':post})
My url route:
path('posts/<int:post_id>/',views.post_detail,name='detail'),
And the line that links to detail page:
<p class="lead my-3">{{ latest_post.title }}</p>
I don't think I should have any error in this. Where am I going wrong?
EDIT
I solved this problem. It's just that when I was trying to add this "detail" link to the "latest_post" variable. But I had specified it only for "post" variable. I just replaced it with "post" and it worked!

Error redirecting after login

I am trying to redirect users to a create-profile form after signup.Since I don't know how to use dynamic url for LOGIN_REDIRECT_URL.I tried this little trick of linking it to a static view then finally to a dynamic view using redirect.It gives the error Reverse for 'add_profile' with arguments '()' and keyword arguments '{'username': u'badguy'}' not found. 0 pattern(s) tried: [] (with 'badguy' truly the username of the just signed-up user').From the error,it is clearly passing the username to the view.My codes are:
settings.py
LOGIN_REDIRECT_URL = '/prof-change/gdyu734648dgey83y37gyyeyu8g/'
urls.py
url(r'^prof-change/gdyu734648dgey83y37gyyeyu8g/',views.login_redir,name='login_redir'),
url(r'^create-profile/(?P<username>\w+)/$',views.add_profile,name='create-profile'),
views.py
def login_redir(request):
user=get_object_or_404(User,username=request.user.username)
username=user.username
return redirect('add_profile',username=username)
def add_profile(request,username):
userman=User.objects.get(username=username)
........
........
When you reverse urls (including when you use a url name with the redirect shortcut), you should use the name of the url pattern, not the view. The url pattern has name='create-profile', so use that.
redirect('create-profile', username=username)

Django How do i determine what arguments are missing?

Trying to access part of code by get request and receiving this error
NoReverseMatch: Reverse for 'homepage' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: [] looked around for help and i think its that am missing some arguments.
here is request am using in my test
req = self.client.get('/password/reset/')
and the code it leads to is as:
class PasswordResetEmailView(FormView):
template_name = 'temp/password_reset.html'
form_class = PasswordResetForm
success_url = 'password_reset_ok'
def get(self, request, *args, **kwargs):
form = self.get_form(self.form_class)
return self.render_to_response(self.get_context_data(form=form))
any guidance ?
You ask specifically about "determining which arguments are missing", but should be irrelevant to your error.
Your error message states that it can't find any URL patterns matching the name "homepage", before attempting any argument matching. Therefore no URLs exist named "homepage" in your setup. If it was an argument mismatch, you'd see a list of named URLs in the error such as "tried X urls."
Either define a URL named home in your URLConf url(r'^home/$', 'home_view', name="homepage") or find the location that is calling reverse('homepage') and remove it. You will find this information in your traceback.

Django NoReverseMatch for a Particular User

This is quite surprising and I can't seem to get my way around it.
The code below works for most users but it breaks when I try to render a link for user SSenior generating the error below:
NoReverseMatch at /tofollow/
Reverse for 'profile' with arguments '(u'SSenior ',)' and keyword arguments '{}' not found. 1 pattern(s) tried: ['user/(?P\w+)/$']
urls.py
url(r'^tofollow/$', views.tofollow, name='tofollow'),
url(r'^user/(?P<username>\w+)/$', views.profile, name='profile'),
template.html
#{{user.username}}
The username has a space in the end of it.
u'SSenior '
The regex \w+ does not match spaces, therefore you get the NoReverseMatch error.
Remove the space (you could use the Django admin to do this) and it will work.

Django url template returns NoReverseMatch

continuing question from here. after fixing the template according to #dannyroa - removing the quotes from the template name I still get NoReverseMatch error:
NoReverseMatch at /transfers/41/
Reverse for 'description_url' with arguments '(u'\u05ea\u05e7\u05e6\u05d9\u05d1 \u05d4\u05e9\u05db\u05e8 - \u05d1\u05d9\u05ea \u05d4\u05e0\u05e9\u05d9\u05d0',)' and keyword arguments '{}' not found.
Request Method: GET
Request URL: http://127.0.0.1:8000/transfers/41/
Django Version: 1.4
Exception Type: NoReverseMatch
Exception Value:
Reverse for 'description_url' with arguments '(u'\u05ea\u05e7\u05e6\u05d9\u05d1 \u05d4\u05e9\u05db\u05e8 - \u05d1\u05d9\u05ea \u05d4\u05e0\u05e9\u05d9\u05d0',)' and keyword arguments '{}' not found.
the template now:
<a href='{% url description_url transfer.description %}'>{{transfer.description}}</a>
the urlconf:
url(r'^description/(?P<description>[\w ]+)/$',
'transfers.views.description_ListView',
name = 'description_url')
the view:
def description_ListView(requesst,**kwargs):
template_name = 'transfers/description.html'
o = get_list_or_404(Transfer, description =kwargs['description'])
#print ('o:',o)
context_object_name = "transfer_name_list"
return render_to_response(template_name,{context_object_name:o,'description':kwargs['description']})
this problem is really starting to get me down. I know I can write a specific method on the Transfer Model (maybe with #permalink) returning the right url for each view. but this is a lot of work and certainly frustrating to do this only because of my miserable failure at using the {%url %} template tag
thanks for the help

Categories