Django DRF APITestCase post url results in 404 error - django

I'm learning DRF test cases, and in my test.py file, my URL in the client post-call is coming back in a 400 status error:
Here's my urls.py:
from django.contrib import admin
from django.urls import path, include
#from rest_auth.views import LoginView, LogoutView
urlpatterns = [
path('admin/', admin.site.urls),
path("api/", include("profiles.api.urls")),
path("api-auth/", include("rest_framework.urls")),
path("api/rest-auth/", include("rest_auth.urls")),
path("api/rest-auth/registration/", include("rest_auth.registration.urls"))
]
from django.conf.urls.static import static
from django.conf import settings
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Here's my test.py file
import json
from django.contrib.auth.models import User
from django.urls import reverse
from rest_framework.authtoken.models import Token
from rest_framework.test import APITestCase
from rest_framework import status
from profiles.models import Profile
from profiles.api.serializers import ProfileSerializer
class RegistrationTestCase(APITestCase):
def test_registration(self):
data = {"username": "testuser1", "email": "test#localhost.app", "password1": "A41&14all", "password2": "A41#14all"}
response = self.client.post("/api/rest-auth/registration/", data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
It appears that the line self.client.post.... fails to find the endpoint.
What am I missing?
thanks!

Related

does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import .Django 3.0.2

Project name : CRMPROJECT
App name: leads
I'm working on a CRM project With Django and I keep getting this error while trying to map my urls to views
django.core.exceptions.ImproperlyConfigured. The included URLconf does not appear to have any patterns in it
Below is the project/urls.py file
'''
from django.contrib import admin
from django.urls import path,include
from leads import views
urlpatterns = [
path('admin/', admin.site.urls),
path('leads/',include('leads.urls',namespace='leads')),
]
'''
Below is leads/urls.py
from django.urls import path
from . import views
app_name = 'leads'
urlpatterns = [
path('',views.home,name='home'),
]
And lastly my leads/views.py
'''
from django.shortcuts import render
from django.http import HttpResponse
# Create your views here.
def home(request):
return render(request,'leads/base.html')
'''

Want to generate Custom 404/500 error page in django app

This error comes by default and I want to customize it
I have tried the code bellow to generate custom error/404/500 page but its not working at all,
actually I want to raise this error when user tries to enter wrong url,then a custom error page with 404/500 would be displayed to the user.
views.py
from django.shortcuts import render
from django.shortcuts import render
from django.conf import settings
def error_404(request,exception):
context = {}
context = {"project name":settings.PROJECT_NAME}
return render(request,'error_404.html',context)
def error_500(request):
context = {}
context = {"project name":settings.PROJECT_NAME}
return render(request,'error_500.html',context)
app 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 django.conf.urls import handler404, handler500, include
from .import views
import testapp
from testapp import views as common_views
urlpatterns = [
path('error_404',views.error_404,'error_404'),
path('error_500',views.error_500,'error_500'),
]
project/urls.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import handler404, handler500, include
from testapp import urls
import testapp
from testapp import views as common_views
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(testapp.urls)),
]
handler404 = common_views.error_404
handler500 = common_views.error_500
It will work when you make DEBUG = False, and do not forget to add ALLOWED_HOSTS = ['localhost', '127.0.0.1']

I'm unable to run server due to this error: 'urls.py'>' does not appear to have any pattern'"

Error:
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'dtapp.urls' from 'D:\\pylearn\\dt\\dtapp\\urls.py'>p.urls' from D:\\pylearn\\dt\\dtapp\\ then the issue is probably caused by a circterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.
urls.py
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.news),
]
views.py
from django.shortcuts import render
from django.http import HttpResponse
def news(request):
return HttpResponse ("n iuri u iou")
urls.py(django)
from django.contrib import admin
from django.conf.urls import url,include
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^dtapp/', include('dtapp.urls'))
]

My url not reading my function in views.py in DJANGO (found similar question not able to trace my issue)

mysite/urls.py
from django.contrib.auth import views
from django.views.generic.base import TemplateView
urlpatterns = [
url('update_db/(<dbname>)/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
url('mysite/login/', auth_views.LoginView.as_view(),name='login'),
url(r'^fetch/',include('polls.urls')),
url('mysite/logout/$', auth_views.logout, name='logout'),
]
polls.urls.py
from django.conf.urls import url
from . import views
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
urlpatterns = [
url('update_db/(?P<dbname>\w+)/$', login_required(views.MyView.get), name='x'),
url('', login_required(views.IndexView.as_view())),
]
fetch.html
<td>click</td>
polls/views.py
from django.shortcuts import render
from django.views.generic import TemplateView , ListView
from django.views.generic.edit import UpdateView
# Create your views here.
from django.http import HttpResponse
from .models import Item , DbRestorationLogDetails
class MyView(UpdateView):
logger.error('Something went wrong!')
print "THANKS FOR UPDATING"
template_name='fetch.html'
model = DbRestorationLogDetails
fields =['verification_status']
def get(self,request, dbname):
usname=request.user
print usname
print dbname
if request.method == "GET":
dblog = DbRestorationLogDetails.objects.get(dbname=dbname)
dblog.verification_status = 'Verified'
print dblog.verification_status
dblog.save()
#.update(dblogdetails.verification_status = 'Verified')
return HttpResponseRedirect(request.GET.get('next'))
NOTE: NOT able to reach to MyView.get() and no db update happening. I assume my code is not able to read my function.

ImportError: cannot import name Upload

I am working on Django, django-rest project and as I have searched the problem, it is beleived I have circular problem, but I don`t think so...
Relevant parts of problem are here:
demo/views.py
from django.shortcuts import render
from rest_auth.models import Upload, UploadForm
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
# Create your views here.
def home(request):
if request.method=="POST":
img = UploadForm(request.POST, request.FILES)
if img.is_valid():
img.save()
return HttpResponseRedirect(reverse('imageupload'))
else:
img=UploadForm()
images=Upload.objects.all()
return render(request,'home.html',{'form':img,'images':images})
rest_auth/models.py
from django.conf import settings
from rest_framework.authtoken.models import Token as DefaultTokenModel
from .utils import import_callable
from django.db import models
from django.forms import ModelForm
class Upload(models.Model):
pic = models.ImageField("Image", upload_to="images/")
upload_date=models.DateTimeField(auto_now_add =True)
# FileUpload form class.
class UploadForm(ModelForm):
class Meta:
model = Upload
and
demo/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.views.generic import TemplateView, RedirectView
from django.conf import settings
from django.conf.urls.static import static
from views import home
from rest_framework_swagger.views import get_swagger_view
import os
PROJECT_DIR=os.path.dirname(__file__)
urlpatterns = [
url(r'^$', TemplateView.as_view(template_name="home.html"), name='home'),
url(r'^upload/$', views.home, name='imageupload'),
url(r'^signup/$', TemplateView.as_view(template_name="signup.html"),
name='signup'),
Problem is as stated this:
File "/Desktop/zadnja/django-rest-auth/demo/demo/urls.py",
line 6, in
from views import home File "/Desktop/zadnja/django-rest-auth/demo/demo/views.py", line
2, in
from rest_auth.models import Upload, UploadForm ImportError: cannot import name Upload
You do not have circular imports since rest_auth/models.py does not import any component from demo/views.py
I'm assuming rest_auth is an app in you project. To import Upload and UploadForm you should use absolute import:
from project_name.rest_auth.models import Upload, UploadForm