I can't seem to spot the error in my code and I have tried everything. It is probably something simple that is escaping my eye. Please help! Any input is appreciated. New Django learner here.
Traceback (most recent call last):
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\doctrinality\doctrinality\Testimony\views.py", line 175, in TestimonyOrderView
return render(request, 'testimony_post.html', {'queryset_list':queryset_list})
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\loader_tags.py", line 188, in render
return template.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 173, in render
return self._render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\template\defaulttags.py", line 442, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\hanya\AppData\Local\Programs\Python\Python37\lib\site-packages\django\urls\resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'details' with arguments '('',)' not found. 2 pattern(s) tried: ['(?P<id>[0-9]+)/details(?P<format>\\.[a-z0-9]+/?)$', '(?P<id>[0-9]+)/details/$']
[16/Jan/2019 21:58:50] "GET /testimonypost/ HTTP/1.1" 500 187935
template
{%for objects in queryset_list%}
<div class="w3-card-4">
<header class="w3-container w3-blue">
<h1><a href="{% url 'details' testimony.id %}"</a>{{testimony.Title}}</h1>
</header>
<div class="w3-container">
<p>{{objects.body}}</p>
</div>
<footer class="w3-container w3-blue">
</footer>
</div>
<form action="{% url 'testimonydelete'%}" method="POST" style="display">
<button class="glyphicon glyphicon-trash"></button>
</form>
{%endfor%}
models.py
class Testimony(models.Model):
...
def __str__(self):
return int(self.id)
urls.py
urlpatterns = [
...
path('<int:id>/details/', views.detail, name='details'),
]
views.py
def details(request, id=None):
print('1')
testimony=get_object_or_404(Testimony, id=id)
print('2')
return render(request, 'details.html', {'testimony': testimony})
The name of the variable in the for loop is actually objects, for some reason. So, just like you use objects.body to display the body, you need objects.id in the URL:
<h1><a href="{% url 'details' objects.id %}"</a>{{ objects.Title }}</h1>
Related
i am trying to reset password but i got this error TemplateDoesNotExist at /blogapp/reset_password/ path_to/reset_password_email.html
urls.py
from django.urls import path
from . import views
from .views import signupview
from django.contrib.auth import views as auth_views
from django.urls import reverse_lazy
app_name='blogapp'
urlpatterns=[
path('',views.home,name='home'),
path('createblog/',views.blogview,name='blogview'),
path('blog/',views.blogretrieve,name='blog'),
path('signup/',views.signupview,name='signup'),
path('login/',views.loginview,name='login'),
path('logout/',views.logoutview,name='logout'),
path('author/<int:pk>/',views.authorview,name='author'),
path('blogdata/<str:pk>/',views.blog,name='blogdata'),
path('profile/<str:pk>/',views.profile,name='profile'),
path('change-password/', auth_views.PasswordChangeView.as_view(template_name='blogapp/change-password.html',success_url=reverse_lazy('blogapp:password_change_done'))),
path('password_change/done/',auth_views.PasswordChangeDoneView.as_view(template_name='blogapp/password_change_done.html'),name='password_change_done'),
path('reset_password/',auth_views.PasswordResetView.as_view(template_name='blogapp/reset_password.html',
success_url=reverse_lazy('blogapp:password_reset_done'),
email_template_name='path_to/reset_password_email.html'),name='reset_password'),
path('reset_password_sent/',auth_views.PasswordResetDoneView.as_view(template_name='blogapp/reset_password_sent.html'),name='password_reset_done'),
path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view( template_name='blogapp/reset_password_form.html',
success_url=reverse_lazy('blogapp:password_reset_complete')),name='password_reset_confirm'),
path('reset_password_complete/',auth_views.PasswordResetCompleteView.as_view(),name='Password_reset_complete'),
]
Request Method: POST
Request URL: http://127.0.0.1:8000/blogapp/reset_password/
Template error:
In template C:\Users\SHAFQUET NAGHMI\blog\blogapp\templates\blogapp\reset_password_email.html, error at line 6
Reverse for 'blogapp/password_reset_confirm' not found. 'blogapp/password_reset_confirm' is not a valid view function or pattern name.
1 : {% load i18n %}{% autoescape off %}
2 : {% blocktranslate %}You're receiving this email because you requested a password reset for your user account at {{ site_name }}.{% endblocktranslate %}
3 :
4 : {% translate "Please go to the following page and choose a new password:" %}
5 : {% block reset_link %}
6 : {{ protocol }}://{{ domain }} {% url 'blogapp/password_reset_confirm' uidb64=uid token=token %}
7 : {% endblock %}
8 : {% translate 'Your username, in case you’ve forgotten:' %} {{ user.get_username }}
9 :
10 : {% translate "Thanks for using our site!" %}
11 :
12 : {% blocktranslate %}The {{ site_name }} team{% endblocktranslate %}
13 :
14 : {% endautoescape %}
Traceback (most recent call last):
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", line 69, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\utils\decorators.py", line 130, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\views.py", line 226, in dispatch
return super().dispatch(*args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\base.py", line 101, in dispatch
return handler(request, *args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\views\generic\edit.py", line 144, in post
return self.form_valid(form)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\views.py", line 239, in form_valid
form.save(**opts)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\forms.py", line 313, in save
self.send_mail(
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\contrib\auth\forms.py", line 256, in send_mail
body = loader.render_to_string(email_template_name, context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 176, in render
return self._render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 168, in _render
return self.nodelist.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 977, in render
return SafeString(''.join([
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render_annotated
return self.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 38, in render
output = self.nodelist.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 977, in render
return SafeString(''.join([
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render_annotated
return self.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\loader_tags.py", line 56, in render
result = self.nodelist.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 977, in render
return SafeString(''.join([
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 978, in <listcomp>
node.render_annotated(context) for node in self
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\base.py", line 938, in render_annotated
return self.render(context)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\template\defaulttags.py", line 442, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\base.py", line 86, in reverse
return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
File "C:\Users\SHAFQUET NAGHMI\AppData\Local\Programs\Python\Python310\lib\site-packages\django\urls\resolvers.py", line 729, in _reverse_with_prefix
raise NoReverseMatch(msg)
Exception Type: NoReverseMatch at /blogapp/reset_password/
Exception Value: Reverse for 'blogapp/password_reset_confirm' not found. 'blogapp/password_reset_confirm' is not a valid view function or pattern name.
Unable to filter using DjangoFilterBackend, Its returns NULL.
While using other attribute like name or value it works fine.
__basic_fields =('name',)
filter_fields = __basic_fields
It returns correct value.
import django_filters
from rest_framework.viewsets import ModelViewSet
from .models import UserDetails
from .serializer import UserDataSerializer
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import ListAPIView
from rest_framework.filters import SearchFilter, OrderingFilter
from rest_framework.pagination import LimitOffsetPagination
class MyDetailViewSet(ModelViewSet):
queryset = putDetails.objects.all()
serializer_class = putDataSerializer
filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter,)
__basic_fields =('stock',)
ilter_fields = __basic_fields
'stock' is Multiselectfield in my models.py.
Should I handle MultiSelect in some other way?
Traceback Traceback (most recent call last): File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
response = self.process_exception_by_middleware(e, request) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\core\handlers\base.py", line 154, in _get_response
response = response.render() File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\renderers.py", line 733, in render
context = self.get_context(data, accepted_media_type, renderer_context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\renderers.py", line 710, in get_context
'filter_form': self.get_filter_form(data, view, request), File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\rest_framework\renderers.py", line 642, in get_filter_form
html = backend().to_html(request, queryset, view) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django_filters\rest_framework\backends.py", line 105, in to_html
return template.render(context, request) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py", line 199, in render
c = self.get_render(context).flatten() File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\templatetags\crispy_forms_tags.py", line 118, in get_render
actual_form.form_html = helper.render_layout(actual_form, node_context, template_pack=self.template_pack) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\helper.py", line 308, in render_layout
template_pack=template_pack File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\layout.py", line 140, in render
return self.get_rendered_fields(form, form_style, context, template_pack, **kwargs) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\layout.py", line 104, in get_rendered_fields
for field in self.fields File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\layout.py", line 104, in <genexpr>
for field in self.fields File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\crispy_forms\utils.py", line 148, in render_field
html = template.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\defaulttags.py", line 309, in render
return nodelist.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\defaulttags.py", line 309, in render
return nodelist.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\defaulttags.py", line 309, in render
return nodelist.render(context) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\template\base.py", line 940, in render
bits.append(str(bit)) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\utils\html.py", line 397, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self)) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\boundfield.py", line 33, in __str__
return self.as_widget() File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\boundfield.py", line 93, in as_widget
renderer=self.form.renderer, File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 229, in render
context = self.get_context(name, value, attrs) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 668, in get_context
context = super().get_context(name, value, attrs) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 629, in get_context
context['widget']['optgroups'] = self.optgroups(name, context['widget']['value'], attrs) File "C:\.virtualenvs\Stock-k36TmZyJ\lib\site-packages\django\forms\widgets.py", line 592, in optgroups
for subvalue, sublabel in choices: ValueError: too many values to unpack (expected 2) [12/Apr/2019 12:52:58] "GET /api/usr/ HTTP/1.1" 500 297265
I'm probably missing something here, but I have a connect page in which the logged in user can chat to another user by clicking on a Chat? link located on each users profile.
The chat functionality is built using Django Channels and is accessed via the url pattern
site/messages/username
where username is the other_user that the current_user is chatting with.
I currently have the chat? link as
<a class='btn btn-light' href="{% url 'thread' username %}" id="chat">Chat?</a>
However that throws the error
NoReverseMatch Reverse for 'thread' not found. 'thread' is not a valid view function or pattern name.
I have tried following the docs and adding username as an arg / taking it off but I keep hitting this same error.
The chat app is included in settings.py and the urls are included in admin.py.
The connect and chat are separate apps.
Below is relevant code. Thank you for your time and help!
chat/urls.py
from django.urls import path, re_path
from .views import ThreadView, InboxView
app_name = 'chat'
urlpatterns = [
path("", InboxView.as_view()),
re_path(r"^(?P<username>[\w.#+-]+)", ThreadView.as_view(), name='thread'),
]
chat/views.py
class ThreadView(LoginRequiredMixin, FormMixin, DetailView):
template_name = 'chat/thread.html'
form_class = ComposeForm
success_url = './'
def get_queryset(self):
return Thread.objects.by_user(self.request.user)
def get_object(self):
other_username = self.kwargs.get("username")
obj, created = Thread.objects.get_or_new(self.request.user, other_username)
if obj == None:
raise Http404
return obj
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['form'] = self.get_form()
return context
traceback
Internal Server Error: /connect/
Traceback (most recent call last):
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/Users/xx/xx/project/src/project/connect/views.py", line 15, in connect_home_view
return render(request, 'connect/home.html', context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/defaulttags.py", line 209, in render
nodelist.append(node.render_annotated(context))
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/template/defaulttags.py", line 442, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/urls/base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "/Users/xx/xx/project/lib/python3.7/site-packages/django/urls/resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'thread' with arguments '('',)' not found. 1 pattern(s) tried: ['messages/(?P<username>[\\w.#+-]+)']
[2019/01/27 12:36:43] HTTP GET /connect/ 500 [0.10, 127.0.0.1:50815]
You're using app_name = 'chat'. This makes your app level urls available under the app namespace. See docs: https://docs.djangoproject.com/en/2.1/topics/http/urls/#url-namespaces-and-included-urlconfs
This should work:
{% url 'chat:thread' username %}
Since you have given your app a name in your url app_name = 'chat'
your url must have the name given
<a class='btn btn-light' href="{% url 'chat:thread' username %}" id="chat">Chat?</a>
this is called namespacing in programming
I have a project folder mysite and my app folder passwordValidate. When I move the path of user_login to passwordValidate/urls.py, the login works but same is not working from mysite/urls.py.
The error message is Reverse for 'user_login' not found. 'user_login' is not a valid view function or pattern name.
mysite/urls.py
from passwordValidate import views
urlpatterns = [
path('',views.home,name='home'),
path('passwordValidate/',include('passwordValidate.urls')),
path('user_login',views.user_login,name='user_login'),
]
passwordValidate/views.py
def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username,password=password)
if user:
if user.is_active:
login(request,user)
return HttpResponseRedirect(reverse('home'))
else:
return render(request,'passwordValidate/login.html')
passwordValidate/templates/passwordValidate/base.html
<li class="nav-item">
<a class="nav-link" href="{% url 'user_login' %}">Log In</a>
</li>
console
[16/Jan/2019 10:00:39] "GET / HTTP/1.1" 200 2555
Not Found: /media/profile_pics/IMG_20181030_161728.jpg
[16/Jan/2019 10:00:39] "GET /media/profile_pics/IMG_20181030_161728.jpg HTTP/1.1" 404 2670
Not Found: /favicon.ico
[16/Jan/2019 10:00:44] "GET /favicon.ico HTTP/1.1" 404 2577
Internal Server Error: /user_login
Traceback (most recent call last):
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Anshul\Desktop\django_lectures\Learning Jango\Django\NEW\PASSWORD VALIDATION\mysite\passwordValidate\views.py", line 74, in user_login
return render(request,'passwordValidate/login.html')
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\loader.py", line 62, in render_to_string
return template.render(context, request)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 171, in render
return self._render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 163, in _render
return self.nodelist.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 937, in render
bit = node.render_annotated(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\base.py", line 904, in render_annotated
return self.render(context)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\template\defaulttags.py", line 442, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\urls\base.py", line 90, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\Anshul\Anaconda3\lib\site-packages\django\urls\resolvers.py", line 622, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'user_login' not found. 'user_login' is not a valid view function or pattern name.
[16/Jan/2019 10:00:47] "GET /user_login HTTP/1.1" 500 148137
This question already has an answer here:
NoReverseMatch following v1.9 to v1.10 upgrade
(1 answer)
Closed 5 years ago.
I am getting the below error message and I couldn't see a way to fix it.
NoReverseMatch at /login/
Reverse for '' not found. '' is not a valid view function or pattern name.
Request Method: GET
Request URL: http://127.0.0.1:8000/login/?next=/
Django Version: 2.0.1
Exception Type: NoReverseMatch
Exception Value:
Reverse for '' not found. '' is not a valid view function or pattern name.
Exception Location: C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 632
Python Executable: C:\Users\gokul\Desktop\TESTING\data\Scripts\python.exe
Python Version: 3.5.2
Python Path:
['C:\\Users\\gokul\\Desktop\\TESTING\\data\\tagent',
'C:\\Users\\gokul\\Desktop\\TESTING\\data\\Scripts\\python35.zip',
'C:\\Users\\gokul\\Desktop\\TESTING\\data\\DLLs',
'C:\\Users\\gokul\\Desktop\\TESTING\\data\\lib',
'C:\\Users\\gokul\\Desktop\\TESTING\\data\\Scripts',
'c:\\program files\\python35\\Lib',
'c:\\program files\\python35\\DLLs',
'C:\\Users\\gokul\\Desktop\\TESTING\\data',
'C:\\Users\\gokul\\Desktop\\TESTING\\data\\lib\\site-packages']
> python manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
January 17, 2018 - 17:21:56
Django version 2.0.1, using settings 'tagent.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
[17/Jan/2018 17:22:03] "GET / HTTP/1.1" 302 0
Internal Server Error: /login/
Traceback (most recent call last):
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\core\handlers\base.py", line 158, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
response = response.render()
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\defaulttags.py", line 447, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\urls\base.py", line 88, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\urls\resolvers.py", line 632, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'django.contrib.auth.views.login' not found. 'django.contrib.auth.views.login' is not a valid view function or pattern name.
[17/Jan/2018 17:22:04] "GET /login/?next=/ HTTP/1.1" 500 153293
I have a login.html template with contains the following:
<form method="post" action="{% url 'django.contrib.auth.views.login' %}">
If I change the url tag as follows, then I am getting another traceback.
<form method="post" action="{% url django.contrib.auth.views.login %}">
Django version 2.0.1, using settings 'tagent.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Internal Server Error: /login/
Traceback (most recent call last):
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\core\handlers\exception.py", line 35, in inner
response = get_response(request)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\core\handlers\base.py", line 158, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\core\handlers\base.py", line 156, in _get_response
response = response.render()
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\response.py", line 106, in render
self.content = self.rendered_content
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\response.py", line 83, in rendered_content
content = template.render(context, self._request)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\backends\django.py", line 61, in render
return self.template.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 175, in render
return self._render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\loader_tags.py", line 155, in render
return compiled_parent._render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 167, in _render
return self.nodelist.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\loader_tags.py", line 67, in render
result = block.nodelist.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 943, in render
bit = node.render_annotated(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\base.py", line 910, in render_annotated
return self.render(context)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\template\defaulttags.py", line 447, in render
url = reverse(view_name, args=args, kwargs=kwargs, current_app=current_app)
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\urls\base.py", line 88, in reverse
return iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs))
File "C:\Users\gokul\Desktop\TESTING\data\lib\site-packages\django\urls\resolvers.py", line 632, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for '' not found. '' is not a valid view function or pattern name.
[17/Jan/2018 17:37:56] "GET /login/?next=/ HTTP/1.1" 500 152780
Could someone give me a pointer on what went wrong?
You should use the URL pattern name, not the dotted python path.
You haven't shown how you are including the login view in your URLs, so we can only guess what the correct name is, but it's probably 'login'.
<form method="post" action="{% url 'login' %}">
Have you try:
<form method="post" action="{% url '/accounts/login/' %}">