0

while working with Django URLs I ran into a problem that i cannot understand.

I will bring 2 examples that are implemented in a similar way but only one is working. The dashboard and users-list (dashboard not working)

urlpatterns = [
    path('', views.home, name='home'),
    path('dashboard/', views.dashboard, name='dashboard'),
    path('users-list/', views.users_list, name='users-list'),
]

The html of both links

<li class="sidebar-item">
   <a class="sidebar-link waves-effect waves-dark sidebar-link"   href="{% url 'dashboard' %}" aria-expanded="false">
      <i class="mdi mdi-av-timer"></i>
      <span class="hide-menu">Dashboard</span>
   </a>
</li>

<li class="sidebar-item">
    <a class="sidebar-link waves-effect waves-dark sidebar-link" href="{% url 'users-temp-records' %}"
      <i class="mdi mdi-account-multiple-outline"></i>
      <span class="hide-menu">Users</span>
    </a>
</li>  

views

@login_required
def dashboard(request):
    user = request.user
    entranceRecords = None
    if user.is_staff == True:
        entranceRecords = TbEntranceRecord.objects.all().order_by("-create_time")
    else:
        entranceRecords = TbEntranceRecord.objects.filter(people_name=user.username).order_by("-create_time")
        

    page = request.GET.get('page', 1)   
    paginator = Paginator(entranceRecords, 10)

    try:
        data = paginator.page(page)
    except PageNotAnInteger:
        data = paginator.page(1)
    except EmptyPage:
        data = paginator.page(paginator.num_pages)
    context = {
        'title': 'Dashboard',
        'records': data,
    }
    return render(request, 'app/common/dashboard.html', context)



@ login_required
def users_temp_records(request):
    user = request.user
    entranceRecords = None
    if user.is_staff == True:
        entranceRecords = TbUserTemperatureRecord.objects.all().order_by("-create_time_date")

    page = request.GET.get('page', 1)
    paginator = Paginator(entranceRecords, 12)

    try:
        data = paginator.page(page)
    except PageNotAnInteger:
        data = paginator.page(1)
    except EmptyPage:
        data = paginator.page(paginator.num_pages)

    context = {
        'title': 'Users Records',
        'records': data,
    }
    return render(request, 'app/admin/users/users_temp_records.html', context)


The error is Reverse for '' not found. '' is not a valid view function or pattern name. when going to http://127.0.0.1:8000/dashboard/

Dashboard has been working until I have tried to give it a filter parameter

it looked like this, I also have a parameter on image url and it works, so I tried the same way to implement the dashboard parameter.

urlpatterns = [
    path('', views.home, name='home'),
    path('dashboard/<str:filter>', views.dashboard, name='dashboard'),
    path('users-list/', views.users_list, name='users-list'),
    path('image/<str:image_md5>', views.image, name='image'),

]

I tried to use the parameter this way

href="{% url 'dashboard' 'create_time' %}"

It did go to the http://127.0.0.1:8000/dashboard/create_time but the error mentioned above has arisen. When I have switched to the implementation above without filter, the error was still there.

kakakakakakakk
  • 467
  • 10
  • 31
  • Check this https://stackoverflow.com/questions/45724006/django-reverse-for-not-found-is-not-a-valid-view-function-or-pattern-na maybe it can help – iamhimanshu0 May 21 '21 at 09:09

1 Answers1

0

I don't know if I understand well, but I can't see your view: users_temp_records in urls.py

try:

path('users_temp_records/', views.users_temp_records, name='users_temp_records'),
HudsonBarroso
  • 455
  • 4
  • 20