I’m taking a Udemy course by Nick Walter and rather than copying line by line, I’m trying to branch out and experiment on my own.
The purpose of the website I am creating is for a small blog, with the ability to redact string input (in an HTML form) from the user. There is also a word counter for the body content of the blog.
I got Django running but as soon as I started adding the code I wrote, Django stopped running properly.
Here is the traceback in full: https://pastebin.com/8HtdNwPP
The main issue shows at the bottom:
File "/home/<user>/dev/projects/python/2018-and-2019/CC_Redact_Iter2/CC_Redact_Iter2/urls.py", line 28, in <module>
path('^james/', views.posts, name='james'),
AttributeError: module 'counters.views' has no attribute 'posts’
Based on this traceback, I gather I have probably misnamed a function or a file name or template but I can’t for the life of me figure which one or where.
My entire source code repo can be found here: https://github.com/Angeles4four/CC_Redact_Iter2
Here are some of the relevant files involved.
urls.py:
from django.contrib import admin
from django.urls import path
# from . import views
from posts import views
from redactors import views
from counters import views
urlpatterns = [
path('admin/', admin.site.urls),
path('^$', views.home, name='home'),
path('^result/', views.result, name='result'),
path('^seth/', views.counters, name='seth'),
path('^james/', views.posts, name='james'),
path('^james/', views.redactors, name='simon'),
]
counters/views.py:
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
if 'ccEntry' in request.GET:
number = request.GET['ccEntry']
redacted_num = 'xxxx xxxx xxxx {}'.format(number[-4:])
return render(request, 'result.html', {'number':number, 'redacted_num':redacted_num})
else:
return render(request, 'home.html')
def result(request):
return render(request, 'result.html')
def counters(request):
return render(request, 'counters/james.html')
Here is my file tree: https://imgur.com/a/BUTKKEH
Contents of requirements.txt:
Django==2.0.13
Pillow==5.4.1
psycopg2==2.7.7
psycopg2-binary==2.7.7
pytz==2018.9
If there are other files in my project that you wish to view, you can click through the file tree as it appears on GitHub (linked to above).
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/353c83b5-1f37-4e23-b6c7-f488cb05805e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thank you Lloyd:
You are right that my import statements are written inside my urls.py in such away that they override each other, confusing Django. I appreciate your advice, but with all due respect, a slightly more Pythonic approach would be this:
“””
from django.contrib import admin
from django.urls import path, re_path
# from . import views
from posts.views import *
from redactors.views import *
from counters.views import *
urlpatterns = [
path('admin/', admin.site.urls),
re_path('^$', home, name='home'),
re_path('^result/', result, name='result'),
re_path('^seth/', counters, name='seth'),
re_path('^james/', posts, name='james'),
re_path('^simon/', redactors, name='simon'),
]
“””
I find the re_path’s as they appear here to be more human readable than single alphabetical letters.
To explain with clarity, lines 4,5,6 import all functions from within the app directories and within each views.py package.
Also, Django 2.0 now requires regex paths in url patterns to use the re_path() function as describe here: https://docs.djangoproject.com/en/2.2/ref/urls/#re-path
My Django server now runs without reporting an issue at system check.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a3d83e7a-2e85-41e1-99a2-7da5228b9e32%40googlegroups.com.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a3d83e7a-2e85-41e1-99a2-7da5228b9e32%40googlegroups.com.