Django NoReverseMatch url issue - django

I'm getting the error
"Reverse for 'recall' with arguments '('',)' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'associate/recall/']"
When I try to submit a form. Here is my html:
<form action="{% url 'associate:recall' ordered_group %}" method="post">
{% csrf_token %}
<div>
<label for="recall">enter as many members of {{ ordered_group }} as you can recall </label>
<input type="text" id="recall" name="recall">
</div>
<div id="enter_button">
<input type="submit" value="enter" name="enter" />
</div>
<div id="done_button">
<input type="submit" value="done" name="done" />
</div>
</form>
"ordered_group" is a model object that is carried over from the 'learn' view:
urls.py:
urlpatterns = patterns('',
url(r'^learn/', "associate.views.learn", name='learn'),
url(r'^recall/', 'associate.views.recall', name='recall'),
url(r'^$', "associate.views.index", name='index'),
)
I am trying to use the ordered_group model object that is submitted in the learn view context to the html, back to the recall view as an argument. Can one do this? It makes sense to me, but what is the correct way of doing this?
views.py
def recall(request, ordered_group):
...
def learn(request):
...
ordered_group = ordered_groups[index]
return render(request, 'associate/learn.html', {'dataset':model, 'ordered_group':ordered_group})
I want to submit the form with

In you HTML, you are doing:
{% url 'associate:recall' ordered_group %}
Django expects that "recall" url is in "associate" namespace, because of the ":". But, you need to declare the namespace in urls.py, like:
url(r'^recall/', 'associate.views.recall', namespace='associate', name='recall')
If you don't want the namespace, just do:
{% url 'recall' ordered_group %}
And, about "ordered_group", you need to declare it in your url, like:
url(r'^recall/(?P<ordered_group>\w+)', 'associate.views.recall', namespace='associate', name='recall')
You are passing ordered_group in HTML, youare expecting this in views.py, but you are not expecting this on you URL.

Related

Why is Django product editing not working. Reverse for 'edit' not found?

I'm trying to edit a product (without using forms.py) but I get an error Reverse for 'edit' not found. 'edit' is not a valid view function or pattern name.
vievs.py
def edit(request, id):
if (request.method == 'POST'):
obj, update = Posts.objects.update_or_create(title=request.POST.get("title"))
obj.text=request.POST.get("text")
obj.date=request.POST.get("date")
obj.image=request.POST.get("image")
obj.save()
return render(request, 'edit.html')
html
<form action="{% url "blog:edit" %}" method="post">
{% for el in posts %}
{% csrf_token %}
<input type="text" placeholder="Название" name="title" value="{{ el.title }}"><br>
<textarea placeholder="Текст статьи" rows="8" cols="80" name="text"></textarea><br>
<input type="file" name="image"><br>
<button type="submit">Добавить статью</button>
{% endfor %}
</form>
You need to define the view in your blog app's urls.py file. Something like this:
urlpatterns = [
# ... other patterns
path('<int:id>/edit/',views.edit,name='edit'),
]

Django Template Second Form Input Triggering Wrong View

Trying to have two forms on one page and have a user select a create input or load input. Should be pretty straight forward. Doesn't seem to work. Anytime I click select a person and click load, it evaluates the first URL for Create. Can someone please point out what I'm missing? I'm sure it's something simple.
Views:
def Create(request):
print('Create View')
def Load(request):
print('Load View')
URLs:
urlpatterns = [
path('', views.Index, name='index'),
path('person/', views.Create, name='Create'),
path('person/', views.Load, name='Load'),
Template:
{% block body_main %}
<form action={% url 'Create' %} method='POST'>
<h2>Name</h2>
{% csrf_token %}
{{ form1 }}
<input class="btn btn-success" name="form1btn" value="Submit" type="submit"/>
</form>
<br>
<form action={% url 'Load' %} method='POST'>
<h2>Select Existing Name</h2>
{% csrf_token %}
{{ form2 }}
<input class="btn btn-success" name="form2btn" value="Submit" type="submit"/>
<form>
{% endblock %}
Here is the problem (also mentioned in point 3 in Django URL docs:
path('person/', views.Create, name='Create'),
path('person/', views.Load, name='Load'),
Everytime django URL matcher gets here, it will usually hit Create first and return.
Django runs through each URL pattern, in order, and stops at the first one that matches the requested URL, matching against path_info.
So it will always return the Create view.
To solve this problem I suggest creating 3 separate URLs that are more readable:
path('person/new', views.Create, name='Create'),
path('person/load', views.Load, name='Load'),

Password Reset functionality inheriting from Django clases - reverse url issues ('uidb64')

I want to create a Password Reset functionality but changing the templates.
So I'm inheriting from Django classes.
After I insert the email to reset password, I ge the following error:
NoReverseMatch at /accounts/password-reset/
Reverse for 'confirm_reset_password' with keyword arguments
'{'uidb64': '', 'token': '4y5-9ae986836e35f95b842c'}' not found. 1
pattern(s) tried:
['accounts\/password-reset-confirm/(?P[0-9A-Za-z_\-]+)/(?P[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$']
I think the issue is that 'uidb64', but I don't know why is empty.
Views:
class CustomPasswordResetView(PasswordResetView):
form_class = CustomPasswordResetForm
email_template_name = 'account/password_reset_email.html'
template_name = 'account/password_reset.html'
class UserPasswordResetConfirmView(PasswordResetConfirmView):
pass
Form:
class CustomPasswordResetForm(PasswordResetForm):
email = forms.EmailField(widget=TextInputWidget)
Urls:
path('password-reset/', UserPasswordResetView.as_view(), name='reset_password'),
re_path(r'^password-reset-confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$',
UserPasswordResetConfirmView.as_view(), name='confirm_reset_password')
in reset template:
<form action="" method="post">
{% csrf_token %}
<div class="row ">
{{ form.email }}
</div>
<div class="l-action">
<input type="submit" class="button" value="Reset my password">
</div>
</form>
In email template:
a href="http://{{ domain }}{% url 'users:confirm_reset_password' uidb64=uidb token=token %}"
I think your email template contains an error. You write:
a href="http://{{ domain }}{% url 'users:confirm_reset_password' uidb64=uidb token=token %}"
But the uidb64 parameter should, according to the documentation [Django-doc] have as parameter the uid variable, so:
a href="http://{{ domain }}{% url 'users:confirm_reset_password' uidb64=uid token=token %}"

django 2.0 work with forms without jquery

from django import forms
class Find(forms.Form):
object_name = forms.CharField()
views.py
def get_obj(request, object_name='000'):
print(object_name)
form = FindSSK()
print(request.GET)
urlpatterns = [
# path(r'ssk/<str:object_name>/', get_obj),
re_path(r'^(?P<object_name>)#$', get_obj),
path(r'', get_obj),
]
{% block find %}
<form class="form-inline ml-5" action="#" method="GET">
{% comment %} {{form}} {% endcomment %}
{% comment %} <input type="number" class="form-control" placeholder="Enter obj" aria-label="Search"> {% endcomment %}
{% comment %} <input type="text" > {% endcomment %}
<input type="text" name="object_name" />
<input class="btn btn-outline-success ml-1 fas fa-search" type="submit" >
</input>
</form>
{% endblock %}
When i push to submit on forms, he will redirect http://127.0.0.1:8000/?object_name=001
But var object_name steal 000
result print in get_obj()
000
<QueryDict: {'object_name': ['001']}>
Sorry for bad English.
You're not able to get the argument needed because you're actually sending the value as a GET argument. However object_name is passed in your view as an argument and as URL parameter for your URL pattern, means that this should be included in the URL like url/object_name/ == http://127.0.0.1:8000/001/. Not sure this is what fits better your need.
To send data to the view, you could use a POST request or a GET request as you did with http://127.0.0.1:8000/?object_name=001.
For both options above, you don't need to have object_name as a parameter neither this ^(?P<object_name>) in url pattern.
VIEW: def get_obj(request object_name='000'):
______________________
URL: re_path(r'^$', get_obj),
method="GET": If you use GET request in form <form class="form-inline ml-5" action="#" method="GET"> you could retrieve the value like the following
def get_obj(request):
object_name = request.GET.get('object_name') # object_name is the field name in form
print(object_name)
method="POST": If you use POST request in form <form class="form-inline ml-5" action="#" method="POST"> you could retrieve the value like the following
def get_obj(request):
object_name = None
if request.method == 'POST':
object_name = request.POST.get('object_name')
print(object_name)
If you use POST request, don't forget to add {% csrf_token %} in your form

Referencing URLs for other views in Django 1.8 with no arguments

I am working on a simple Django 1.8 application with a template that referencesa url that points to another view. The views are all class-based generic views.
When attempting to load up the page, I get the following error:
NoReverseMatch at /
Reverse for 'add' with arguments '()' and keyword arguments '{}' not found. 1 pattern(s) tried: [u'$add/$']
<form action="{% url 'movies:add' %}" method="post">{% csrf_token %}
My app's url.py:
urlpatterns = [
url(r'^$', views.MovieList.as_view(), name="index"),
url(r'^add/$', views.MovieCreate.as_view(), name="add"),
url(r'^(?P<pk>\d+)/delete/$', views.MovieDelete.as_view(), name="delete"),
url(r'^(?P<pk>\d+)/update/$', views.MovieUpdate.as_view(), name="update")
]
The template being used:
<table>
<tr>
<td>Title</td>
<td>Year</td>
<td>Director</td>
</tr>
{% for movie in movie_list %}
<tr>
<td>{{movie.title}}</td>
<td>{{movie.year}}</td>
<td>{{movie.director}}</td>
</tr>
{% endfor %}
</table>
<form action="{% url 'movies:add' %}" method="post">{% csrf_token %}
<input type="submit" value="Add Movie" />
</form>
The two views in question:
class MovieList(ListView):
model = Movie
queryset = Movie.objects.order_by("title", "-year", "director")
context_object_name = "movie_list"
template_name = "movies/index.html"
class MovieCreate(CreateView):
model = Movie
fields = ["title", "year", "director"]
template_name = "movies/add_movie.html"
This is strange to me since the correct URL is being resolved in the template and there are no variables expected by the CreateView (I think).
1 pattern(s) tried: [u'$add/$']
This means that the regex used to include your app's urls ends with a $. It works for an empty pattern such as your MovieList, but not for non-empty patterns. Remove the $ in that regex and it'll work.