I want to add a main page with a login but I have the error
A server error occurred. Please contact the administrator.
what does it means ??
url:
urlpatterns = patterns('',
url(r'^ยง', main_page),
url(r'^mainView/$', mainView.as_view()),
url(r'^material/(?P<slug>[-\w]+)/$', MaterialDetailView.as_view(), name='material_material_detail'),
)
view:
def main_page(request):
template = get_template('registration/main_page.html')
variables = Context({ 'user': request.user })
output = template.render(variables)
return HttpResponse(ouput)
Pretty sure you meant
url(r'^$', main_page),
Related
Main urls.py file:
urlpatterns = [
path(
'admin/',
admin.site.urls
),
path(
'',
include(
'employee.urls'
)
),
path(
'',
include(
'epos.urls'
)
),
path(
'',
include(
'supplier.urls'
)
),
]
epos.urls:
urlpatterns = [
path(
'',
home_view,
name='home'
),
home_view:
#login_required
def home_view(request):
# Get all categories
categories = Category.objects.all()
# Get the first category which will be selected by default
selected_category = categories.first()
# Get the order_items for the selected category (first category)
products = Product.objects.filter(
category=selected_category.id
)
user = request.user
tax = Tax.objects.latest('id')
context = {
'categories': categories,
'user': user,
'title': "EPOS",
'products': products,
'selected_category': selected_category,
'tax': tax
}
return render(request, 'epos/epos.html', context)
Test case:
class LoginTests(StaticLiveServerTestCase):
fixtures = ['fixtures/employee/employee_data.json']
def setUp(self):
self.browser = webdriver.Chrome()
self.browser.get(self.live_server_url)
self.browser.maximize_window()
def tearDown(self):
self.browser.quit()
def test_login_when_clocked_in(self):
import ipdb;ipdb.set_trace()
login_button = self.browser.find_element_by_xpath(
'//button[normalize-space()="Login"]'
)
clock_in_out_button = self.browser.find_element_by_xpath(
'//button[normalize-space()="Clock In/Out"]'
)
pin_input = self.browser.find_element_by_id(
'pin'
)
pin_code = 'some pin'
employee_name = 'some name'
pin_input.send_keys(pin_code)
clock_in_out_button.click()
time.sleep(10)
login_button.click()
settings.py
ROOT_URLCONF = 'allPOS.urls'
LOGIN_URL = '/login/'
When the test logs in I am redirected to the main webpage which is localhost:46497/ but instead of the page I am getting Server Error 500.
This error occurs only when testing. In addition, if I add another path e.g. localhost:46497/analytics it opens the webpage as expected.
Any help would be appreciated.
After 4 hours of debugging, I found out that the issue was related to the database being empty.
The webpage that I wanted to render is expecting some models to be passed, and since the DB was empty those models weren't passed and hence the crash. In normal circumstances ( ./manage.py runserver) if DEBUG=True it will tell you what is wrong, but for some reason StaticLiveServerTestCase doesn't have this option or at least I am not aware of it. If anyone knows how to enable some debugger for the StaticLiveServerTestCase please feel free to add to this thread.
So what I did:
./manage.py dumpdata order.tax > data.json - I dumped the data that I needed into a json file and at the beginning of the TestCase I added that data into the fixture
Hope this helps someone with the same issue!
So I have a page which I want to render after the details section in my project. My urls.py is below:
from django.conf.urls import url
from . import views
app_name = 'main'
urlpatterns = [
url(r'^home/', views.home, name='home'), # Home page
url(r'incubators/$', views.incubators, name='incubators'), # Incubator list page
url(r'about/', views.about, name='about'), # Websie about page
url(r'results', views.result, name = 'result'), # For search function
url(r'incubators/(?P<incubator_id>[0-9]+)/', views.details, name = 'details'), # shows details of incubators
url(r'incubators/add-incuabtor/$', views.AddIncubator.as_view(), name = 'add-incubator'), # Adding Inc
url(r'/add-details/', views.AddDetails.as_view(), name = 'add-details'), #for additional details
url(r'news/', views.news, name = 'news'),
url(r'added/', views.added, name = 'added'), #your incubator will be added soon page
url(r'apply/', views.apply, name = 'apply'),
url(r'done/', views.done, name = 'done'),
url(r'location/', views.location, name = 'location'),
url(r'prediction/', views.prediction, name = 'prediction'),
url(r'^/locate/', views.locate, name = 'locate'),
]
I want to open a page (present in the details.html) which will be showing some info (in my case latitude and longitude) of that particular element.
Following is my views.py
def details(request, incubator_id):
inc = get_object_or_404(Incubators, pk = incubator_id)
details = Details.objects.get(pk = incubator_id)
return render(request, 'main/details.html', {'inc': inc, 'details': details})
def locate(request):
locate = get_object_or_404(Incubators, pk = incubator_id)
return render(request, 'main/locate.html', {'locate': locate})
But I am getting the following error:
NameError at //locate/
name 'incubator_id' is not defined
Request Method: GET
Request URL: http://127.0.0.1:8000//locate/
Django Version: 1.11.3
Exception Type: NameError
Exception Value:
name 'incubator_id' is not defined
Exception Location: C:\Users\my dell\Desktop\Project\junityme\main\views.py in locate, line 137
Python Executable: C:\Users\my dell\AppData\Local\Programs\Python\Python36-32\python.exe
Using the URLconf defined in Ekyam.urls, Django tried these URL patterns, in this order:
I think I am doing some mistake in making urls for this. Help would be appriciated.
http://127.0.0.1:8000//locate/ would be looking for a URL pattern that starts with "locate," but you don't have any URL patterns that start with locate. The URL should match the pattern, not the name you give it, or the view name.
My redirect function is causing some issues. I call a url from a view using reverse with the parameters required for the view. There are no errors and in the browser of the url it correctly displays these parameters. However it seems like it redirects to the new url, but immediately after requesting the new view for the new url the page returns to the original view with the new url still displayed in the browser. Can anyone tell me if I am using the redirect function correctly or maybe I am using the reverse incorrectly?
P.S. I chopped out a lot of code because StackOverflow won't let me post all of it.
home/urls.py
from django.conf.urls import url
from home import views
app_name = 'home'
urlpatterns = [
url('^$', views.index, name='index'),
url('^patient_summary/patientid=(?P<patient_id>\d+)&clinicid=(?P<clinic_id>\d+)/', views.patient_summary, name='patient_summary'),
url('^patient_summary/patientid=(?P<patient_id>\d+)&clinicid=(?P<clinic_id>\d+)/careplanid=(?P<careplan_id>\d+)/', views.care_plan, name='care_plan'),
]
home/views.py
def patient_summary(request, token, patient_id, clinic_id):
user = get_user(token)
if request.method == "POST":
if ('careplanselected' in request.POST):
props = request.POST.get('careplan')
props = props.split("#")
CPID = props[0]
cpname = props[1]
my_dict = {'token': token, 'patient_id': patient_id, 'clinic_id': clinic_id, 'careplan_id': CPID}
return redirect(reverse('home:care_plan', kwargs=my_dict))
return render(request, 'home/patient_summary.html')
def care_plan(request, token, patient_id, clinic_id, careplan_id):
user = get_user(token)
care_plan = []
cpname = ''
return render(request, 'home/care_plan.html' )
Your URL patterns are missing dollars to mark the end of the URL. That means that your patient_summary view will be handling requests meant for the care_plan view.
Change the patterns to:
url('^patient_summary/patientid=(?P<patient_id>\d+)&clinicid=(?P<clinic_id>\d+)/$', views.patient_summary, name='patient_summary'),
url('^patient_summary/patientid=(?P<patient_id>\d+)&clinicid=(?P<clinic_id>\d+)/careplanid=(?P<careplan_id>\d+)/$', views.care_plan, name='care_plan'),
I'm trying to use this link <a href="Annotation/{{ vod.id }}"> to load another page in my website base on primary key of videos. My url file is as follows:
urlpatterns = [
url(r'^$', ListView.as_view(queryset=Vod.objects.all().order_by("-date")[:25], template_name="Annotation/home.html")),
url(r'^(?P<pk>\d+)$', ListView.as_view(queryset=Vod.objects.get(pk=1).posts.all().order_by("-date"), template_name="Annotation/post.html")),
]
I get the standard 404 from the links generated using the aforementioned link.
Thanks!
edit: Added the base URLs
url(r'^admin/', admin.site.urls),
url(r'^Annotation', include('Annotation.urls')),
url(r'^Profile', include('Profile.urls')),
This is the URL for Profile.urls
url(r'^$', views.index, name='index'),
edit2: Changed URL and added view I'm trying to use.
url(r'^(?P<key>[0-9]+)$', views.post, name="post")
Here's views.post
def post(request, key):
try:
target_vod = Vod.objects.get(pk=key)
except Target.DoesNotExist:
raise Http404("Vod does not exist")
target_posts = Vod.objects.get(pk=key).posts.all().order_by("-date")
context = {'target_vod': target_vod, 'target_posts': target_posts}
return render(request, 'Annotation/post.html', context)
I have seperated the code and put it into a views.py. This should work.
urls.py
url(r'^(?P<pk>[0-9]+)$', AnnoList.as_view(), template_name="Annotation/post.html")),
Then in views.py:
class AnnoList(ListView):
template_name = 'Annotation/post.html'
def get_queryset(self):
self.vod = get_object_or_404(Vod, pk=self.args[0])
return Posts.objects.filter(vod=self.vod).all().order_by("-date")
I assuming here that you have two different tables and that Posts has a foreign key to Vod.
I am very new to Python and Django. I am trying to redirect to a second view function. Here is my url configuration:
urlpatterns = patterns('dept.pv.verif.views',
(r'^apps/dept/pv/verif/$', 'index', {}, 'index'),
(r'^apps/dept/pv/verif/display$', 'display', {}, 'display'),
(r'^apps/dept/pv/verif/display/(?P<action>\w{1})/(?P<id>\w{8})/$', 'display', {}, 'display'),
url(r'^apps/dept/pv/verif/display/(?P<action>\w{1})/(?P<id>\w{8})/$', 'display', name='display'),)
And here are my view functions:
def index(request):
context = {}
visit_switch = request.GET.get('visit_switch')
if not visit_switch:
id_form = Enter_ID()
else:
id_form = Enter_ID(request.GET)
if id_form.is_valid():
return redirect('display', action='R', id='test')
context['id_form'] = id_form
return render_to_response('index.html', {'context':context})
and the second:
def display(request, action, id):
# ...
return render_to_response('index.html')
I'm getting a NoReverseMatch error. I don't understand why the redirect line is not matching up with one of my urls. I would appreciate any help you can offer.
This regular expression group:
(?P<id>\w{8})
Will only match something 8 characters long. If you're actually passing id='test', that would be your problem.