Django Server Unable to Open Admin Page - python

I am following an online course on Django, and my browser fails to load the admin page. I created a job, added it to INSTALLED_APPS in settings.py, and added it in admin.py as follows:
from .models import Job
# Register your models here.
admin.site.register(Job)
In urls.py I believe I have the standard admin url
urlpatterns = [
path('admin/', admin.site.urls),
path('home/', jobs.views.home, name='home'),
]
I'm not sure if any other any information is needed but please ask if there is anything else. I start the server using python manage.py runserver and it can access /home okay, but if I change the url to admin or admin/ it says 'Firefox can’t establish a connection to the server at 127.0.0.1:8000.'.I've tried it on different browsers and the same outcome is reached. My question is similar to the one here: Django error browser is unable to connect at 127.0.0.1:8000/products/ and I tried all of the solutions there but none worked. I followed the tutorial very specifically, so I have no idea what is causing this. I am using Atom on a Windows 10 machine, the project is connected to a postgresql database.
UPDATE/EDIT 08/23/20 10:31AM PST.
Here is a screenshot of INSTALLED_APPS:
There is no error in console when I try to open admin. If I run the server as shown below and open the link http://127.0.0.1:8000/admin, the server just stops working and my command line returns to just taking in commands. Details in screenshot below.
UPDATE/EDIT 08/23/20 12:07PM PST.

Related

how to use django 3 with django-otp to send token sms using SMS service provider for user verification and redirecting to password reset form?

I have only been able to make the following changes in django code:
settings.py:
added along with other apps added in
INSTALLED_APPS = [
.....
'django_otp',
'django_otp.plugins.otp_totp',
]
In additions to other middleware configurations, added:
MIDDLEWARE = [
'django_otp.middleware.OTPMiddleware',
]
urls.py:
from django_otp.admin import OTPAdminSite
from django_otp.plugins.otp_totp.models import TOTPDevice
admin_site = OTPAdmin(name='OTPAdmin')
admin_site.register(User)
admin_site.register(TOTPDevice)
urlpatterns = [
path('admin/', admin_site.urls), #otp app
path('dadmin/', admin.site.urls),
]
Then I ran: $ python3 manage.py migrate otp_totp --fake
and runserver. Created a user and totp device. Scanned the qr code in google authenticator. Then tried logging in using the admin url to login for this new user. It asks for the token generated which I input, it says invalid token though user is authenticated. Seen other posts where the secret code needs to be converted to 32basecode etc, but don't know exactly and where to implement. What more code needs to be added to get this working? I will require detailed code and steps for my use case where i need to change the time for generating the code and send via sms using my service provider api and redirect to password reset form.
Using django 3.1, django-otp 1.0.2
My google authenticator works with my gmail account, so there is no clock time difference either.

django-rest-auth: Issue with Password Reset functionaliity

I have been trying to setup password reset functionality in DRF using django-rest-auth. Earlier I was getting error TemplateDoesNotExist:registration/password_reset_email.html which I resolved by adding the following code
serializer.py
from rest_auth.serializers import PasswordResetSerializer
from allauth.account.forms import ResetPasswordForm
class PasswordSerializer(PasswordResetSerializer):
password_reset_form_class = ResetPasswordForm
settings.py
REST_AUTH_SERIALIZERS = {
'PASSWORD_RESET_SERIALIZER': 'api.serializers.PasswordSerializer',
}
However, Now I am getting into another issue - "NoReverseMatch: Reverse for 'account_reset_password_from_key' not found. 'account_reset_password_from_key' is not a valid view function or pattern name.". And haven't found any solution or workaround for this.
Any help would be appreciated.
So, finally I got the password reset functionality working. Here is how it goes -
We just need one URL in our urls.py -
urlpatterns = [
url(r'^account/', include('allauth.urls')),
url(r'^rest-auth/', include('rest_auth.urls')),
# This is the only URL required for BASIC password reset functionality.
# This URL creates the confirmation link which is sent via e-mail. All of the rest
# password reset features get their reverse lookup via django-allauth and django-rest-auth.
url(r'^password-reset/confirm/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', TemplateView.as_view(), name='password_reset_confirm'),
url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', allauthemailconfirmation,
name="account_confirm_email"),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls'), name='account_signup'),
]
Using this URL configuration raised TemplateDoesNotExist at /api/rest-auth/password/reset/ error first. After a lot of debugging, I found that the issue was raised for the template - registration/password_reset_email.html which resides under the Django Admin's template directory. This happened due to another Django app that I was using and it had disabled the django admin app.
So, adding 'django.contrib.admin' under INSTALLED_APPS and removing the serializers resolved the issue.
I hope this resolves issue for others as well.
PS: Debugger is your best friend. ;)

How to prevent URLconf settings from being carried onto new project?

I recently worked on a project that had an app named 'catalog'. While I was working on it, I changed the URLconf so that the root URL could be redirected to the app.
I wrote the below code to do this:
# within the project's URLconf file
from django.views.generic import RedirectView
urlpatterns += [
url(r'^$', RedirectView.as_view(url='/catalog/', permanent=True)),
]
I was working on the local development server, and the root URL (127.0.0.1:8000) was successfully redirected to the 'catalog' app (127.0.0.1:8000/catalog/).
However, when I created a new project, the root URL of this NEW project ALSO tried to redirect to the 'catalog' app of the previous project.
So where as I should be seeing the "it worked!" page at the root URL for the new project, I am instead redirected to the 'catalog' app's URL of the previous project, where the 404 page is displayed (obviously, because the 'catalog' app is not part of the new project).
Instead of this:
It Worked Page
Seeing this:
404 Page
It seems to me that the settings from the previous project have somehow affected the local server permanently so that the modified URLconf setting is carried on to any subsequent projects.
I could not find exactly what was causing this issue so I just ran the new project on a different port (8001) using the
python manage.py runserver 8001
command, and this seemed to fix the issue. However, I regard this only as a temporary workaround and I want to find out the root cause of the issue.
If I can't fix it, I would like to "reset" the default port (8000) so everything goes back to default settings.
Is there a way to completely "reset" either the local server or django itself so that all the settings go back to how they were released?

The current URL, app/, didn't match any of these

I'm a newbie in Django and just started looking at it before a day by installing Django 1.10 on my local.
I've followed all the instructions of this link https://docs.djangoproject.com/en/dev/intro/tutorial01/. However I'm continuously getting this error:
Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/polls/
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:
^admin/
The current URL, polls/, didn't match any of these.
I've created polls app to getting started.
To make a start, I went with view.py, here is the code:
polls/views.py
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
def index(request):
return HttpResponse("Hello World!")
Here is my code for urls.py:
polls/urls.py
from django.conf.urls import patterns, url
from . import views
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
)
And here is the urls.py in root:
mysite/urls.py
from django.conf.urls import patterns,include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^polls/', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
I've spent good piece of time to find out the solution, found that this question has been asked for many times,so tried with those solutions as well but none of them worked for me.
I can't find out what I'm missing here so please draw my attention to the gap.
I think you have edited the wrong file when trying to change the root url config.
Make sure you are editing the root url config in mysite/mysite/urls.py (the directory containing settings.py) not mysite/urls.py (the directory containing manage.py).
As general advice, install the latest release, currently 1.9. Don't use 1.10, which is under development. Make sure that you are following the tutorial for 1.9, because the tutorial changes for different versions. For example, your mysite/urls.py doesn't match the tutorial for 1.9, as the urlpatterns should be:
urlpatterns = [
url(r'^polls/', include('polls.urls')),
url(r'^admin/', admin.site.urls),
]
In settings.py you have a setting name INSTALLED_APPS-
Adds you app i.e. polls to it.
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
....
'polls',
]
It's working. Go to your url bar and type your app name:
http://127.0.0.1:8000/home
My app name is home. It will work.
If you want to set your app as your default page then import views from your app.
Like
from home import views
then write
url(r'^$', views.index),
inside.
It will set the views as your default page
http://127.0.0.1:8000/
When you type this it will redirect to your views.
I had the same problem as described. Running a Windows machine.
It turned out, that the virtual environment I was using had not been configured properly (or maybe I had forgot to activate it before installing Django) and the interpreter and django-admin were fetched from the wrong path by CMD.EXE.
If it appears as if Django is "ignoring" your urls.py - try deleting everything, re-creating the virtual environment, activating it and re-installing Django afterwards.
Make sure you have path("admin/", admin.site.urls) in your main url settings.
change the mysite/url
from django.conf.urls import patterns,include, url
from django.contrib import admin
urlpatterns = patterns('',
url(r'^&', include('polls.urls')),
url(r'^admin/', include(admin.site.urls)),
)
Then run your server and visit 127.0.0.1/8000.
This should take you to the index of your website.
or you leave your code as it is and run 127.0.0.1/8000/polls on your browser
if #Alasdair answer does not work, and it seems your working on correct files, just restart your server
The path in Django 2.2 does not support regular expression , I was using, path('(?P<id>\d+)/share/', views.mail_send_view) , so getting error, now changed to this, path('<int:id>/share/', views.mail_send_view). Now not getting any error. For more info please follow django's official documentation.
I too had same problem going through the official docs tutorial. I was using cloud 9. What I realised before I was able to solve this problem was that while creating the workspace I already chose django(ie by the time my workspace was created, django had already been installed) And going through the tutorial, I again executed $ django-admin startproject mysite thereby having another layer of django with multiple directories and unavoidably the confusion. My solution was to delete everything and start all over.
Checklist:
Make sure to run the server
>>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
February 01, 2020 - 15:04:46
Django version 3.0.2, using settings 'corona.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
Open the browser with cmd running, http://127.0.0.1:8000/polls
should be added to the url.

Linking django object log to a project

I trying to add to my project django log object to my django project
I followed these steps
1)pip install django-object-log
2)copied the object_log folder into your Django project.
3)Add "object_log" to INSTALLED_APPS
4)Run ./manage.py syncdb
5)add this url to my project urls ....url(r'yasmina', include('object_log.urls')),
But i don't know what to do next so any help plz ??
and this is one of the urls in url.py of the django object log ...url(r'^user/(?P\d+)/actions/?$', 'list_user_actions', name="user-object_log-actions"),
when i runned the server and added this url to my browser http://localhost:8000/yasmina/user/2/actions/
I got error this url doesnt match
Looks like your Url pattern matches
http://localhost:8000/user/2/actions/.

Categories