i am using this model extending from abstractuser but even after successfully creating user i am not able to get the tokens....
Tokens are genrerated only for those which i created through python manage.py createsuperuser ..others not working.
why????
i am using simplejwt authentication.
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class user_model(AbstractUser):
email=models.CharField(max_length=200,blank=True)
age=models.CharField(max_length=200,blank=True)
dob=models.CharField(max_length=200,blank=True)
phone=models.CharField(max_length=200,blank=True)
def __str__(self):
return self.username
urls.py
from django.contrib import admin
from django.urls import path,include
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
)
urlpatterns = [
path('admin/', admin.site.urls),
path('api-auth/', include('rest_framework.urls')),
path('signup/',include('userdata.urls')),
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('loggedIn/',include("notes.urls")),
]
Related
urls.py
from django.contrib import admin
from django.urls import path,include
from django.urls import re_path
from App.views import *
from.router import router
from django.views.generic import TemplateView
from rest_framework_simplejwt.views import ( TokenObtainPairView,TokenRefreshView)
from django.conf.urls.static import static
from django.conf import settings
from django.contrib.auth import views as auth_views
urlpatterns = [
path('', TemplateView.as_view(template_name="social_app/index.html")), #social_app/index.html
path('admin/', admin.site.urls), #admin api
path('api/',include(router.urls)), #api
path('accounts/', include('allauth.urls')), #allauth
re_path('rest-auth/', include('rest_auth.urls')), #rest_auth
path('api-auth/', include('rest_framework.urls')),
re_path('/registration/', include('rest_auth.registration.urls')),
path('api/token/', MyObtainTokenPairView.as_view(), name='token_obtain_pair'),
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
path('jobview/',job),
path('timelog/',timelogview),
path('chaining/', include('smart_selects.urls')),
path('admin/password_reset/',auth_views.PasswordResetView.as_view(),name='admin_password_reset',),
path('admin/password_reset/done/',auth_views.PasswordResetDoneView.as_view(),name='password_reset_done',),
path('reset/<uidb64>/<token>/',auth_views.PasswordResetConfirmView.as_view(),name='password_reset_confirm',),
path('reset/done/',auth_views.PasswordResetCompleteView.as_view(),name='password_reset_complete',),
] + static(settings.STATIC_URL, document_root = settings.STATIC_ROOT)
I have given the password reset function in the admin page login, it is showing as "Forgotten your password or username?" in the page but when I click it the url changes http://127.0.0.1:8000/admin/login/?next=/admin/password_reset/ to this but the same login page is loading i didnt redirected to any other reset page.
I have tried but couldn't able to fix it, kindly help me to fix this issue.
Add all urls with admin before path('admin/', admin.site.urls)
Then add this line of code in your admin.py file and run the server and connect with your admin account then go to Site model and edit domain example.com with http://127.0.0.1:8000 when you're in development (in production it will be your server link) and edit name example.com with whatever name you want for your site.
from django.contrib.sites.models import Site
from django.contrib import admin
#admin.register(Site)
class SiteAdmin(admin.ModelAdmin):
list_display = ('domain', 'name')
search_fields = ('domain', 'name')
I have got a CRUD App, I'm learning API rest with Python and Django... I create this module:
from django.contrib.auth.models import User, Group
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model: User
fields = ['url', 'username', 'email', 'groups']
And after that, I write this:
from django.contrib.auth.models import User, Group
from rest_framework import viewsets
from djangoCRUD.Api.serializer import UserSerializer
class UserViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows users to be viewed or edited.
"""
queryset = User.objects.all().orderby('-date_joined')
serializer_class = UserSerializer
This is all right, but, when I write my url.py Here, I have got a problem:
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from djangoCRUD.Api import views
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
And I have this error message:
File "C:\PythonDev\pythoncrud\venv\djangoCRUD\djangoCRUD\urls.py", line 20, in
from djangoCRUD.Api import views
ModuleNotFoundError: No module named 'djangoCRUD.Api'
Try the following code:
from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
# from djangoCRUD.Api import views
from djangoCRUD.Api.views import UserViewSet
router = routers.DefaultRouter()
# router.register(r'users', views.UserViewSet)
router.register(r'users', UserViewSet)
urlpatterns = [
path('admin/', admin.site.urls),
path('', include(router.urls)),
path('api-auth/', include('rest_framework.urls', namespace='rest_framework'))
]
I am using django 2.2.2 . I am trying to include path('api/', include('music.urls')),into my root url but I get an exception thrown from resolvers.py file.
Here is my music/urls.py
urls.py
from django.urls import path
from . import views
app_name = 'music'
urlpatterns = [
path('songs/', ListSongsView.as_view(), name = 'songs-all'),
]
here is my root url file
urls.py
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('music.urls')),
]
views.py
from django.shortcuts import render
from rest_framework import generics
from .models import Songs
from serializers import SongSerializer
# Create your views here.
class ListSongsView(generics.ListApiView):
queryset = Songs.objects.all()
serializer_class = SongsSerializer
models.py
from django.db import models
# Create your models here.
class Songs(models.Model):
title = models.CharField(max_length=255, null = False)
artist = models.CharField(max_length=50, null= False)
def __str__(self):
return "{} - {}".format(self.title, self.artist)
and my stacktrace
File "/home/brianonchari/Documents/django/drf/myapi/lib/python3.5/site-
packages/django/urls/resolvers.py", line 588, in url_patterns
raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'rest.urls' 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.
music/urls.py
from django.urls import path
from .views import ListSongsView
app_name = 'music'
urlpatterns = [
path('songs/', ListSongsView.as_view(), name='songs-all'),
]
root urls.py:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('music.urls')),
]
music/views.py:
from django.shortcuts import render
from rest_framework import generics
from .models import Songs
from .serializers import SongSerializer
# Create your views here.
class ListSongsView(generics.ListAPIView):
queryset = Songs.objects.all()
serializer_class = SongSerializer
music/models.py:
from django.db import models
# Create your models here.
class Songs(models.Model):
title = models.CharField(max_length=255, null=False)
artist = models.CharField(max_length=50, null=False)
def __str__(self):
return "{} - {}".format(self.title, self.artist)
music/serializers.py:
from rest_framework import serializers
from .models import Songs
class SongSerializer(serializers.ModelSerializer):
class Meta:
model = Songs
fields = ('title', 'artist')
run your migration for Songs model:
python manage.py makemigrations
python manage.py migrate
I'm trying to create an api that uploads an image with an email to the database. But I'm getting an error "raise ImproperlyConfigured(msg.format(name=self.urlconf_name))" Is the problem in my urls.py?
https://imgur.com/OjPUhOv.jpg
This is how my structure looks
https://imgur.com/TW6pKPn.jpg
This is the error
for urls.py-
from django.contrib import admin
from django.urls import path,include
from django.conf import settings
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('user.urls')),
path('api/',include('api_test.urls'))
# path('articles/',include('articles.urls'))
]
for api_test/urls.py
from django.urls import path,include
from django.conf import settings
from . import views
from rest_framework import routers
router = routers.DefaultRouter()
router.register('image_test',views.api_test,base_name='image_test')
urlpatterns = [
# path('/',views.api_test),
path('',include(routers.url)),
]
for views.py
class api_test(viewsets.ModelViewSet):
queryset = fineDB.objects.all()
serializer_class = fineSerializer
##for serializers.py
from rest_framework import serializers
from .models import fineDB
class fineSerializer(serializers.ModelSerializer):
image = serializers.ImageField(max_length=None,use_url=True)
class Meta:
model = fineDB
fields = {'email','image'}
You should probably get the urls from router, not routers.
At the same time you don't need both the router and the urlpatterns in that file. You can import the router and mount it's router.urls in urls.py.
from rest_framework import routers
router = routers.DefaultRouter()
router.register('image_test',views.api_test,base_name='image_test')
urlpatterns = [
# path('/',views.api_test),
path('',include(router.urls)), # <-
]
I have got an 404 error with djangocms, Raised by: cms.views.details.
I am trying to insert an external app in django CMS. When I run the app separately I dont have the 404 error on my detail view, all is working fine. But when I put my apps in djangocms, please note that the listview works fine and the detail view makes the 404 error.
I don't know what I'm doing wrong.
djangocms version 3.2
django 1.9
python 3.4
here the url.py of my external app
from django.conf.urls import patterns, url
from . import views
from .views import DocListView, DocDetailView
app_name = 'inventaire'
urlpatterns = patterns('',
url(r'^document/(?P<pk>[0-9]+)/$', views.DocDetailView.as_view(), name='detail'),
url(r'^document$', views.DocListView.as_view(), name='index'),
)
Here the views
from django.shortcuts import get_object_or_404, render
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.views import generic
from .models import Document, Mention
# Create your views here.
class DocListView(generic.ListView):
template_name = 'inventaire/index.html'
context_object_name = 'latest_document_list'
def get_queryset(self):
"""Return the last five published questions."""
return Document.objects.all
class DocDetailView(generic.DetailView):
model = Document
template_name = 'inventaire/detail.html'
here my url.py from my cms
# -*- coding: utf-8 -*-
from __future__ import absolute_import, print_function, unicode_literals
from cms.sitemaps import CMSSitemap
from django.conf import settings
from django.conf.urls import * # NOQA
from django.conf.urls.i18n import i18n_patterns
from django.contrib import admin
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
admin.autodiscover()
urlpatterns = i18n_patterns('',
url(r'^admin/', include(admin.site.urls)), # NOQA
url(r'^sitemap\.xml$', 'django.contrib.sitemaps.views.sitemap',
{'sitemaps': {'cmspages': CMSSitemap}}),
url(r'^select2/', include('django_select2.urls')),
url(r'^', include('cms.urls')),
url(r'^inventaire/', include('testTemplates.apps.inventaire.urls')),
)
# This is only needed when using runserver.
if settings.DEBUG:
urlpatterns = patterns('',
url(r'^media/(?P<path>.*)$', 'django.views.static.serve', # NOQA
{'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),
) + staticfiles_urlpatterns() + urlpatterns # NOQA
The 'cms.urls' include must be the last in your urlpatterns. It will catch all requests, so move your 'inventaire/' include above that.