django admin send back to homepage - python

i am new to django and i am makeing my first site i tried to get on my admin for my site but when i go to 127.0.0.1/admin it sends me back to the home page i have look over my code multiple times and i just cant find anything wrong about it
here is my mysite urls
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
# this is mysite not main
from django.conf.urls import url,include
from django.contrib import admin
#url('^admin/', admin.site.urls),
urlpatterns = [
url(r'^admin/', admin.site.urls),
url('', include('main.urls')),
url('tinymce/', include('tinymce.urls')),
]
here is my main.urls
"""mysite URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/1.11/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: url(r'^$', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.conf.urls import url, include
2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
"""
# this is the main not mysite
from django.conf.urls import url
from django.contrib import admin
from . import views
app_name = 'main'
urlpatterns = [
url('',views.homepage, name="homepage"),
url("register/", views.register,name='register'),
]
urlpatterns = [
url("register/", views.register,name='register'),
url('',views.homepage, name="homepage"),
]
this is my views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse
from django.shortcuts import render, redirect
from .models import cooking
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login, logout, authenticate
# Create your views here.
def homepage(request):
return render(request=request,template_name='main/home.html',context={"cooking": cooking.objects.all})
def register(request):
if request.method == "POST":
form = UserCreationForm(request.POST)
if form.is_valid():
user = form.save()
login(request, user)
return redirect("main:homepage")
else:
for msg in form.error_messages:
print(form.error_messages[msg])
form = UserCreationForm()
return render(request,"main/register.html",context={"form":form })
please tell me if you need more info thankyou

Change this url('tinymce/', include('tinymce.urls', ,namespace = "main")) to your url mysite

Related

AttributeError at /accounts/register/ 'str' object has no attribute '_meta'

I'm getting
AttributeError at /accounts/register/
'str' object has no attribute '_meta'
I could have a spelling mistake in some of my files.
urls.py
(In project)
"""QuestionTime URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
from django_registration.backends.one_step.views import RegistrationView
from users.forms import CustomUserForm
# https://django-registration.readthedocs.io/en/3.1.2/activation-workflow.html
urlpatterns = [
path('admin/', admin.site.urls),
path("accounts/register/", RegistrationView.as_view(
form_class="CustomUserForm",
success_url="/",
), name="django_registration_register"),
path("accounts/", include("django_registration.backends.one_step.urls")),
path("accounts/", include("django.contrib.auth.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")),
]
forms.py
(in app)
from django_registration.forms import RegistrationForm
from users.models import CustomUser
class CustomUserForm(RegistrationForm):
class Meta(RegistrationForm.Meta):
model = CustomUser
django_registration.forms and django_registration.backends.one_step.views gets highlighted yellow in VSCODE

The current path, imageupload, didn't match any of these, Page not Found Django

I am new to Django and I am trying to build an image classifier app, I just tried building the app but I am getting this error, how may I resolve it?
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/imageupload
Using the URLconf defined in imageclassifier.urls, Django tried these URL patterns, in this order:
imageupload ^$ [name='home']
admin/
The current path, imageupload, didn't match any of these.
You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.
These are all changes I made in my app:
This is the views.py file in imgUpload app
from django.shortcuts import render
# Create your views here.
def home(request):
return render(request, 'home.html')
This is the urls.py file in imageUpload app
from django.contrib import admin
from django.urls import path,include
from . import views
urlpatterns = [
path('^$', views.home, name ='home'),
]
And this is the urls.py in the Imageclassifier folder:
"""imageclassifier URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('imageupload', include('imgUpload.urls')),
path('admin/', admin.site.urls),
]
Also, I have added a home.html file in the templates folder. How may I fix this error?
In the latest django path methods, You do not need to use ^$ in your path. Simply remove that ^$ from your path and it will work fine.

Recieving an Attribute error when trying to add a new view

Im learning django, and following a tutorial online, im trying to create a new view for "home" but i keep recieving an attribute error. AttributeError: module 'pages.views' has no attribute 'home'
Ive checked that I'm not doing anything wrong, and so I thought maybe the tutorial is outdated etc, but in the urls.py file, it says to do the exact same thing.
Ive searched around google and tried removing spaces before the comas, changing the import name to a ".", Ive tried changing the name of the function and in the urls.
views.py:
from django.http import HttpResponse
from django.shortcuts import render
# Create your views here.
def home(*args, **kwargs):
return HttpResponse("<h1>Hello World!</h1>")
urls.py:
"""trydjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from pages import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
]
I want to change the home view to just say "hello world" but its returning the attribute error, if i remove the "path('', views.home, name='home')," it works fine (with the default home page)
I think you are importing a wrong path of views.py.
Try this :
"""trydjango URL Configuration
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.2/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from . import views #Import views file from your current root directory
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.home, name='home'),
]
Sorry I didn't check, but hope this works. Apologies if it doesn't help your case.
A view in django is a callable that receives a request and return a response.
So, I think you should add a parameter request to your view.
Try this:
def home(request,*args, **kwargs):
return HttpResponse("<h1>Hello World!</h1>")

Django After login succeed, redirect again to login page

My Django project has 5 apps: app1, app2 ......
each app has one template index.html and 1 view named index.
project/urls.py
from django.conf.urls import url, include
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', include('main.urls')),
url(r'^accounts/', include('django.contrib.auth.urls')),
app1/urls.py
from django.conf.urls import url, include
from . import views
from django.contrib.auth import views as auth_views
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^accounts/login/$', auth_views.login, name='login'),
app1/views.py
from django.shortcuts import render
from django.template import loader
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required, user_passes_test
def is_member(user):
return user.groups.filter(name='group1').exists()
#login_required
#user_passes_test(is_member)
def index(request):
getting our template
template = loader.get_template('app1/index.html')
rendering the template in HttpResponse
return HttpResponse(template.render())
setting.py
LOGIN_URL = '/accounts/login/'
LOGIN_REDIRECT_URL = '/'
The 5 apps requirer login based on group
app one I have no problem to login but the reset of the apps after login succeed, I will be redirected to login page again.
The reset of the apps has exactly the same code except the group name and template location.
Also the logger result show me that there is no problem with credential.
I tried to change the group nae for other app as app1 and still I am getting same result.
Please advice me how to solve this problem.

How to configure Django views and urls to render specific templates

When I bring up 127.0.0.1:8000, the current page that show up is something.html template.
I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html).
What would be the structure to achieve this?
I frequently get error message : The current URL didn't match any of these.
Currently, my structure is
project
---myapp
---admin.py
---models.py
---url.py
---views.py
---static
---templates
---myapp
---html files
---mysite
---settings.py
--- url.py
under my settings.py
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
under mysettings/url.py
urlpatterns = [
# Examples:
url(r'^$', 'mysite.views.home', name='home'),
url(r'^myapp/', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf.urls import patterns
from django.views.generic import TemplateView
urlpatterns = [
# Examples:
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
admin.site.site_header = 'Admin'
myapp/url.py
from . import views
from django.conf.urls import patterns, include, url
from django.views.generic import TemplateView
urlpatterns = patterns('',
#brings up something.html when we comment it out. No module found if included.
#url(r'^$', views.home, name='home'),
url(r'^$', views.post_list, name='post_list'),
)
Include urls of your myapp app in mysite/urls.py
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
urlpatterns = [
url(r'^', include('myapp.urls')),
url(r'^admin/', include(admin.site.urls)),
]
Now, all urls, starting by 127.0.0.1:8000, will check if there is a view that can handle the request. I recommend you to read this to understand how Django URL dispatcher works : [Django documentation - URL Dispatcher] (https://docs.djangoproject.com/en/1.8/topics/http/urls/).
2. Add new route in your myapp.urls:
from django.conf.urls import url, patterns
from . import views
urlpatterns = patterns('',
url(r'^$', views.home, name='home'),
url(r'^something$', views.something, name='something'),
url(r'^posts$', views.post_list, name='post_list'),
)
Now :
127.0.0.1:8000/ will executed code of views.home
127.0.0.1:8000/something will executed code of views.something
127.0.0.1:8000/posts will executed code of views.post_list
Let's define these view now
3: Define the views in myapp.views :
from django.shortcuts import render
def home(request):
"""
Return home page
"""
return render(request, 'myapp/home.html')
def something(request):
"""
Return something page
"""
return render(request, 'myapp/something.html')
def post_list(request):
"""
Return something page
"""
# do what you want
Add your templates in myapp/templates/myapp/. Add home.html and something.html.
Now forget, the `.html
You create a url (with attached view to it).
In the view you render any html page you want.
If you use function based views your 'mysite.views.home' may look like:
def home(request):
...
return render(request, 'path/to/index.html')
and so on.
It's the basics so I won't talk about this much. You can find a good tutorial about mapping urls to views there.

Categories

Resources