def checkupdateapp(request): appversion = str(request.GET.get('appversion')) lastapp =…
- User
def check_update_app(request): app_version = str(request.GET.get('app_version')) last_app = AppVersions.objects.all().order_by('-id').first() last_app_version = last_app.version_id if app_version != last_app_version: return Response(data={'url_path':'account/show_update_app/'}, status=status.HTTP_200_OK) else: return Response(data={}, status=status.HTTP_404_NOT_FOUND) من سورس زیر رو نوشتم ووقتی url مربوط بهش رو فراخوانی میکنم ارور زیر رو میده File "c:\Users\Administrator\Desktop\HomeFood.venv\Lib\site-packages\rest_framework\response.py", line 59, in rendered_content assert renderer, ".accepted_renderer not set on Response" ^^^^^^^^ AssertionError: .accepted_renderer not set on Response HTTP GET /account/check_update_app/?app_version=H-1.2 500 [0.02, 151.240.244.40:19307]
- Assistant
The error you're encountering (
AssertionError: .accepted_renderer not set on Response) typically arises when Django REST Framework (DRF) is unable to determine the appropriate renderer for formatting the response. This usually happens because the view is not properly configured to use DRF's or a similar mechanism that sets the . - User
app_name = 'app_account' urlpatterns = [ path('viewsets/', include(router.urls), name='account_viewsets'), path('otp', OTPView.as_view(), name='otp_view'), path('check_update_app/', check_update_app, name='check_update_app'), path('download_file/int:app_id/', download_file, name='download_file'), path('check_access_token_start/', check_access_token_start, name='check_access_token_start'), ] @api_view(['GET']) def check_update_app(request): app_version = str(request.GET.get('app_version')) last_app = AppVersions.objects.all().order_by('-id').first() last_app_version = last_app.version_id if app_version != last_app_version: download_url = last_app.get_download_url() print(download_url) return Response(data={'download_url': download_url}, status=status.HTTP_200_OK) else: return Response(data={}, status=status.HTTP_404_NOT_FOUND)
- Assistant
To modify the
download_urlso that it omits the leading slash, you can adjust theget_download_urlmethod in yourAppVersionsmodel. The method currently uses Django'sreversefunction to construct the URL, which generates a URL path starting with a slash by default.To achieve your goal, you can manually construct the URL path without the leading slash. Here's how you can change the
get_download_urlmethod to achieve this:pythondef get_download_url(self): # Use reverse to construct the URL, then remove the leading '/' url_path = reverse('app_account:download_file', args=[self.id]) # Remove the leading slash, if exists if url_path.startswith('/'): url_path = url_path[1:] return url_path - User
فازسی بگو و کد خودم رو درست کن
- Assistant
بله، متوجه شدم. برای حذف خط مورب از ابتدای مسیر URL، میتوانید به سادگی با اسلایس کردن رشته حاصل از
reverse، خط مورب را حذف کنید. به این شکل:pythondef get_download_url(self): # استفاده از reverse برای ساختن URL و سپس حذف خط مورب اول url_path = reverse('app_account:download_file', args=[self.id]) # چک کردن و حذف خط مورب در ابتدای رشته return url_path.lstrip('/')در این کد، از روش
lstrip('/')استفاده کردهام تا هرگونه خط مورب (در صورت وجود) در ابتدا حذف شود. این کار به شما لینکهایی مانندaccount/download_file/4/میدهد.این کار به شما کمک میکند تا URL های دستی را بدون خط مورب داشته باشید. البته به این نکته توجه داشته باشید که استفاده از URL بدون خط مورب معمولاً بهصورت نسبی تلقی میشود و ممکن است رفتار متفاوتی را در مسیرهای مختلفی از اپلیکیشن شما ایجاد کند. لذا این تغییر ممکن است در مکانهای مختلف باعث رفتار متفاوتی شود.