v
vdl05551July 3, 2019, 6:14 a.m.

Редирект с page=1 на главную

Добрый день.
Пытаюсь сделать редирект с site.ru/?page=1 на просто site.ru/ и ничего не выходит... я уж и так и сяк. Если страница не существует, то с этим нет проблем, а тут не выходит(( Помогите

We recommend hosting TIMEWEB
We recommend hosting TIMEWEB
Stable hosting, on which the social network EVILEG is located. For projects on Django we recommend VDS hosting.

Do you like it? Share on social networks!

10
Evgenii Legotckoi
  • July 3, 2019, 1:16 p.m.

Добрый день

Вы можете попробовать в get методе попробовать обработать данную ситуацию следующим образом.

def get(self, request, *args, **kawrgs)
    if int(self.request.GET.get('page')) == 1:
        return redirect(self.request.path)
    v
    • July 4, 2019, 6:20 a.m.
    • (edited)

    Наверно я туп((
    Или я не вкуриваю куда и что добавлять))
    Исправил опечатки, добавил код в views, но ничего не происходит. В urls что то надо добавлять?

    def get(self, request, *args, **kwargs):
        if int(self.request.GET.get('page')) == 1:
            return redirect(self.request.path)
    

    Django 2.2.3

      Evgenii Legotckoi
      • July 4, 2019, 6:21 a.m.

      покажите urls и views, а также перезапустите проект, если у вас DEBUG = False

        v
        • July 4, 2019, 6:37 a.m.

        DEBUG = True

        urls.py
        
        from django.conf import settings
        from django.conf.urls.static import static
        from django.contrib import admin
        from django.urls import path
        from .views import index
        from .views import save_detail
        #from .views import redirect_index
        #from .views import get
        from core.views import *
        
        
        urlpatterns = [
            path('', index, name='index'),
            path('save-<slug:slug>.html', save_detail, name='save_detail_url'),
            #path('?page=1', redirect_index),
            #path('page=1', get),
            path('admin/', admin.site.urls),
        ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
        
        views.py
        
        from django.shortcuts import render
        from core.models import Save
        from django.core.paginator import Paginator
        from django.shortcuts import get_object_or_404
        
        
        from django.shortcuts import redirect
        
        
        def index(request):
            saves = Save.objects.all().order_by('-pub_date')
            paginator = Paginator(saves, 22)
        
            page = request.GET.get('page', 1)
            saves = paginator.get_page(page)
            return render(request, 'index.html', context={'saves': saves})
        
        
        def save_detail(request, slug):
            #saves = Save.objects.get(slug__iexact=slug)
            saves = get_object_or_404(Save, slug__iexact=slug)
            return render(request, 'save.html', context={'saves': saves})
        
        
        def get(self, request, *args, **kwargs):
            if int(self.request.GET.get('page')) == 1:
                return redirect(self.request.path)
        
          Evgenii Legotckoi
          • July 4, 2019, 6:42 a.m.

          А понятно... вы вместо Class Based View (CBV) используете просто функции.

          Ну напишите так

          def index(request):
              if int(self.request.GET.get('page')) == 1:
                  return redirect(self.request.path)
          
              saves = Save.objects.all().order_by('-pub_date')
              paginator = Paginator(saves, 22)
          
              page = request.GET.get('page', 1)
              saves = paginator.get_page(page)
              return render(request, 'index.html', context={'saves': saves})
          
            v
            • July 4, 2019, 6:58 a.m.

            NameError: name 'self' is not defined

              Evgenii Legotckoi
              • July 4, 2019, 6:58 a.m.
              def index(request):
                  if int(request.GET.get('page')) == 1:
                      return redirect(request.path)
              
                  saves = Save.objects.all().order_by('-pub_date')
                  paginator = Paginator(saves, 22)
              
                  page = request.GET.get('page', 1)
                  saves = paginator.get_page(page)
                  return render(request, 'index.html', context={'saves': saves})
              
                v
                • July 4, 2019, 7:01 a.m.

                TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'

                  Evgenii Legotckoi
                  • July 4, 2019, 7:02 a.m.
                  • The answer was marked as a solution.
                  def index(request):
                      if request.GET.get('page') and int(request.GET.get('page')) == 1:
                          return redirect(request.path)
                  
                      saves = Save.objects.all().order_by('-pub_date')
                      paginator = Paginator(saves, 22)
                  
                      page = request.GET.get('page', 1)
                      saves = paginator.get_page(page)
                      return render(request, 'index.html', context={'saves': saves})
                  
                    v
                    • July 4, 2019, 7:10 a.m.

                    Огромное спасибо! Прям как в тех. поддержку свою проблему писал)) Даже как то не удобно было :( С зарплаты обязательно поддержу проект!

                      Comments

                      Only authorized users can post comments.
                      Please, Log in or Sign up
                      AD

                      C ++ - Test 004. Pointers, Arrays and Loops

                      • Result:50points,
                      • Rating points-4
                      m

                      C ++ - Test 004. Pointers, Arrays and Loops

                      • Result:80points,
                      • Rating points4
                      m

                      C ++ - Test 004. Pointers, Arrays and Loops

                      • Result:20points,
                      • Rating points-10
                      Last comments
                      i
                      innorwallNov. 15, 2024, 2:27 a.m.
                      Release of C++/Qt and QML application deployment utility CQtDeployer v1.4.0 (Binary Box) optionally substituted alkoxy, optionally substituted alkenyloxy, optionally substituted alkynyloxy, optionally substituted aryloxy, OCH, OC H, OC H, OC H, OC H, OC H, OC H, O C CH, OCH CH OH, O…
                      i
                      innorwallNov. 14, 2024, 9:26 p.m.
                      Qt/C++ - Lesson 031. QCustomPlot – The build of charts with time buy generic priligy We can just chat, and we will not lose too much time anyway
                      i
                      innorwallNov. 14, 2024, 7:03 p.m.
                      Qt/C++ - Lesson 060. Configuring the appearance of the application in runtime I didnt have an issue work colors priligy dapoxetine 60mg revia cost uk August 3, 2022 Reply
                      i
                      innorwallNov. 14, 2024, 12:07 p.m.
                      Circuit switching and packet data transmission networks Angioedema 1 priligy dapoxetine
                      i
                      innorwallNov. 14, 2024, 11:42 a.m.
                      How to Copy Files in Linux If only females relatives with DZ offspring were considered these percentages were 23 order priligy online uk
                      Now discuss on the forum
                      i
                      innorwallNov. 14, 2024, 3:39 a.m.
                      добавить qlineseries в функции priligy amazon canada 93 GREB1 protein GREB1 AB011147 6
                      i
                      innorwallNov. 11, 2024, 10:55 a.m.
                      Всё ещё разбираюсь с кешем. priligy walgreens levitra dulcolax carbs The third ring was found to be made up of ultra relativistic electrons, which are also present in both the outer and inner rings
                      9
                      9AnonimOct. 25, 2024, 9:10 a.m.
                      Машина тьюринга // Начальное состояние 0 0, ,<,1 // Переход в состояние 1 при пустом символе 0,0,>,0 // Остаемся в состоянии 0, двигаясь вправо при встрече 0 0,1,>…

                      Follow us in social networks