Django views rendering just one page and wont render the rest - python

So im using django 1.8 on macosx and i have problem while configuring html, namely when i try to load another page except one that is set at default one(index is the default one), it just refreshes the default one i have set in urls.py and i cant access any other page except that one but in the url bar i can see that im accesing the proper html file because it says so but the page is not changing....heres my code:
app/urls.py-----------
urlpatterns = [
url(r'^contact/', views.contact, name='contact'),
url(r'^projects/', views.projects, name='projects'),
url(r'^services/', views.services, name='services'),
url(r'^', views.index, name='index'),
url(r'^about/', views.about, name='about'),
these are all the pages im trying to install
main urls.py-------------
from app import views
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^',include('app.urls')),
]
and this is my views.py-----------
def contact(request):
return render(request, 'app/template/contact.html',{})
def about(request):
return render(request, 'app/template/about.html',{})
def projects(request):
return render(request, 'app/template/projects.html',{})
def services(request):
return render(request, 'app/template/services.html',{})
def index(request):
return render(request, "app/template/index.html",{})

https://docs.djangoproject.com/en/1.8/intro/tutorial03/
you need $ to end the string. In your case he link all thats starts with all.
url(r'^$', views.index, name='index'),
you views.py:
def contact(request):
return render(request, 'app/contact.html',{})
def about(request):
return render(request, 'app/about.html',{})
def projects(request):
return render(request, 'app/projects.html',{})
def services(request):
return render(request, 'app/services.html',{})
def index(request):
return render(request, "app/index.html",{})

Related

Change URL paramaters parameters in request by id

I want to make urls with GET url pattern
example.com/blog/?id=1
my current code
views.py
def home(request,blog_uuid):
blogs = Blog.objects.get(pk=blog_uuid)
return render(request,'home.html',{'blogs':blogs})
url pattern
path('blog/<blog_uuid>/',views.home,name='Blog'),
my current url now like this example.com/blog/1
Change your url pattern to,
urlpatterns = [
path('blog/', views.home, name='Blog'),
...,
]
thrn change your view as
from django.shortcuts import get_object_or_404
def home(request):
blog_uuid = int(request.GET.get("id", "-1"))
blog = get_object_or_404(Blog, pk=blog_uuid)
return render(request, 'home.html', {'blogs': blog})

Django somehow tries to render the wrong template

I am working on a project using Python 3.6 and Django 2.0.5. And I have a template named register_client.html that I would like to use to render a simple page that will register new clients to the Client model.
The following is my complete urlpatterns:
urlpatterns = [
path('admin/', admin.site.urls),
re_path(r'^logout/$', views.user_logout, name='logout'),
re_path(r'^$', views.home, name='home'),
re_path(r'^home/$', views.home, name='home'),
re_path(r'^about/$', views.about, name='about'),
re_path(r'^blog/$', views.blog, name='blog'),
re_path(r'^contact/$', views.contact, name='contact'),
re_path(r'^lawsuits/$', views.lawsuits_list, name='lawsuits'),
re_path(r'^my_profile/$', views.my_profile, name='my_profile'),
re_path(r'^register_client/$', views.register_client, name='register_client'),
re_path(r'^client/(\d+)/$', views.client, name='client'),
re_path(r'^client/(\d+)/lawsuits/$', views.lawsuits_list, name='lawsuits'),
re_path(r'^client/(\d+)/edit/$', views.edit_client, name='edit_client'),
re_path(r'^client/(\d+)/remove/$', views.remove_client, name='remove_client'),
re_path(r'^lawsuit/(\d+)/$', views.lawsuit, name='lawsuit'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
The pattern that should catch a request to http://localhost/register_client should be re_path(r'^register_client/$', views.register_client, name='register_client').
And here's the view that should render the template appropriately:
def register_client(request):
if request.user.is_authenticated and request.user.is_staff:
if request.method == "POST":
form = ClientForm(request.POST, request.FILES)
if form.is_valid():
# register a new client
...
else:
print(form.errors)
form = ClientForm()
return render(request, 'register_client.html', {'form': form})
return HttpResponseRedirect('/home/') # not_permitted view
With the above configurations, Django's giving me a NoReverseMatch at /register_client/. And it further states that it is looking up for the view with the name client, which is an entirely different view in my views.py - Reverse for 'client' with arguments '('',)' not found. 1 pattern(s) tried: ['client/(\\d+)/$']
I am running out of ideas and places to look for as to what could be the cause of this. Any help or tip will be highly appreciated, thanks in advance.

Not Found: /accounts/register/accounts/register

Page not found (404)
Request Method: POST
Request URL: http://127.0.0.1:8000/accounts/register/accounts/register
views.py :
from django.contrib.auth.decorators import login_required
from django.shortcuts import render, redirect
from custom_user.forms import CustomUserCreationForm
from django.contrib import auth
from django.http import HttpResponseRedirect
#Create your views here
def home(request):
return render(request, "home.html")
def login(request):
c = {}
c.update(csrf(request))
return render(request, "login.html", c)
def about(request):
context = locals()
template = 'about.html'
return render(request,template,context)
#login_required
def userProfile(request):
user = request.user
context = {'user': user}
template = 'profile.html'
return render(request,template,context)
def auth_view(request):
username = request.POST.get['username', '']
password = request.POST.get['password', '']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(request, user)
return HTTpResponseRedirect('account/login')
else:
return HTTpResponseRedirect('account/login')
def register(request):
if request.method == 'POST':
form = CustomUserCreationForm(request.POST)
if form.is_valid():
form.save()
return redirect ('accounts/register_success.html')
else:
form = CustomUserCreationForm()
args = {'form': form}
return render(request, 'accounts/register.html', args)
def register_success(request):
return render(request, 'accounts/register_success.html')
def logout(request):
auth.logout(request)
return render(request, 'logout.html')
when i try to register a new user this error is raised . i manage to create my own custom registration form. i still cannot register any new user . is this error means that my registration form is not authenticate ? can someone explain why i get this error please ? im confused . help me please :(
urls.py :
from django.conf import settings
from django.conf.urls.static import static
from django.conf.urls import url, include
from django.contrib import admin
from profiles import views as profiles_views
from contact import views as contact_views
from checkout import views as checkout_views
from register import views as register_views
urlpatterns = [
url(r'^admin/',include(admin.site.urls)),
url(r'^$', profiles_views.home, name='home'),
url(r'^profile/$', profiles_views.userProfile, name='profile'),
url(r'^about/$', profiles_views.about, name='about'),
url(r'^checkout/$', checkout_views.checkout, name='checkout'),
url(r'^contact/$', contact_views.contact, name='contact'),
url(r'^accounts/register/$', register_views.register, name='register'),
url(r'^accounts/register_success/$', register_views.register_success, name='register_success'),
url(r'^accounts/', include('allauth.urls')),
url(r'^auth/', include('django.contrib.auth.urls')),
]
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root= settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root= settings.MEDIA_ROOT)
Form.valid() should be inside the post indentation

Unable to start 2nd view function

Currently, im trying to make it so that I can complete a question on a page then when it's submitted to the database the next page will load. furthermore, every time I point it towards the templates folder it is piggybacking off the original one meaning that it can't find the new HTML page.
My idea is that when question1 is completed it will link to question2 where the def question2 code will execute and so on. But the forms won't display correctly and I believe it is due to the def question2 not running correctly.
def question1(request):
question_form1 = QuestionForm1()
if request.method == 'POST':
form = QuestionForm1(request.POST)
if form.is_valid():
form.save() # saves to database
return HttpResponse('question2.html')
else:
return render(request, 'music/failed.html')
return render(request, 'music/question1.html', locals())
def question2(request):
question_form2 = QuestionForm2()
if request.method == 'POST':
form2 = QuestionForm2(request.POST)
if form2.is_valid():
form2.save() # Saves to database
return render(request, 'music/question3.html', locals())
else:
return render(request, 'music/failed.html')
return render(request, 'music/question2.html', locals())
Edit:Added Urls.py
from django.conf.urls import url
from . import views
app_name = 'music'
urlpatterns = [
url(r'^$', views.index, name='index'),
url(r'^register/$', views.register, name='register'),
url(r'^login_user/$', views.login_user, name='login_user'),
url(r'^logout_user/$', views.logout_user, name='logout_user'),
url(r'^question1/$', views.question1, name='question1'),
url(r'^question2/$', views.question2, name='question2'),
]
from django.http import HttpResponseRedirect
from django.urls import reverse
# delete following line
return HttpResponse('question2.html')`
# replace with this one
return HttpResponseRedirect(reverse('view_name_here'))
# or if you are using any namespaces for your url
return HttpResponseRedirect(reverse('namespace:view_name_here'))

Django - urls.py and views.py and resolving error 404

I'm fairly new to django. I completed the django project tutorial, and took away a good understanding of it, at least I thought I did. My understanding of how urls.py and views.py are related is urls.py points to the function of views.py and looks for a file to execute based on a page.
Here is my file structure:
The root folder is right above the "friendfinder" folder called "ff"
The 404 error I have shows on all 3 static html pages: index, friends, and contacts.
Here is what my urls.py page looks like:
from django.conf.urls import url
from gather import views
urlpatterns = [
url(r'^interest/', views.InterestView.as_view(), name="interest"),
url(r'^$', views.index, name="index"),
url(r'^friends/', views.friends, name="friend"),
url(r'^contact/', views.contact, name="contact"),
]
This is my views.py page:
import json
from django.http import HttpResponse
from django.http import JsonResponse
from django.shortcuts import render
from django.views import View
from gather.models import Interest
def index(request):
all_interests = Interest.objects.all()
return render(
request,
' gather/index.html',
{
'stuff': 'TEST',
'interests': all_interests
}
)
def friends(request):
return render(request, 'gather/friends.html')
def contact(request):
return render(request, 'gather/contact.html')
class InterestView(View):
def post(self, request):
try:
json_data = json.loads(request.body.decode('utf-8'))
new_interest = Interest(
name=json_data['name'],
description=json_data['description'],
approved=False
)
new_interest.save()
return HttpResponse('ok')
except ValueError:
return HttpResponse('Fail', status=400)
def get(self, request):
try:
search = request.GET.get("search", None)
interests = Interest.objects.filter(name__icontains=search)
interest_json = []
for interest in interests:
interest_json.append({
'name': interest.name,
'description': interest.description
})
return JsonResponse({'search_result': interest_json})
except ValueError:
return HttpResponse('404 Page Not Found', status=400)
On the settings.py page I have under installed apps, I added the app named 'gather', and
ROOT_URLCONF = friendfinder.urls
I also took a look at this page:urls.py and views.py information
I thought maybe my problem was a file structure issue.
Any pointers, hints, tips,and/or explanations of why the urls.py and views.py page are not matching each other's information would be greatly appreciated.
EDIT: Code for Friendfinder/urls.py
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^', include('gather.urls', namespace='gather', app_name='gather'))
]

Categories

Resources