1. You could solve this by aliasing your imports, e.g.:
```
from django.contrib import admin
from django.urls import path
# from . import views as a
from posts import views as b
from redactors import views as c
from counters import views as d
urlpatterns = [
path('admin/', admin.site.urls),
path('^$', a.home, name='home'),
path('^b/', views.result, name='result'),
path('^b/', views.counters, name='seth'),
path('^d/', views.posts, name='james'),
path('^d/', views.redactors, name='simon'),
]
```
... although this is not what you should be doing with django.
2. (much better) you could use the `include` function of the django.urls module and pass in the string representation of each urls module's name. You would not need to import each app's urls.py module for this.
path('^james', include('posts.urls')),
...etc.
I would recommend doing a short Python tutorial to understand some key basics, such as how imports work (https://www.w3schools.com/python/default.asp) and doing the official Django "Getting Started" guide and tutorial (you can find the latest version on https://www.djangoproject.com/start/).
--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.9If 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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@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/CAH-SnCDi3o8%3DbmbPm9MMMn5AGvU8yK_dwR0TjW_SQG2UYatDkQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment